Android Question How can I delete empty line in text file after List.Sort?

TonyVerberne

Member
Licensed User
Longtime User
I use the following code to item entered into a ExitText and append to an existing text file. I then sort it alphabetically. This all works perfectly but when I write the final text file (line 7 of the code below) it always finishes with a blank line. Can anyone help me delete this blank line. List.Size does not count the empty line either.

Here is the code:

Sub EditText1_EnterPressed
outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
outFile.Write (CRLF & Text2)
outFile.Close
List1.Initialize
List1 = File.ReadList(File.DirDefaultExternal, Text3)
List1.Sort(True) 'Sort list in descending alphabetical order
File.WriteList(File.DirDefaultExternal, Text3, List1)
EditText1.Text = ""
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
1. Use code tags when posting code!
2. Why are you creating a new thread for your problem?
3. @Troberg already gave you the solution. So. After you read the list check for emtpy lines - and delete them if there are any - before you want to sort it.

Most probably it is better to post your problematic project (In the ide File-Export as ZIP) and upload this zip here describing what EXACTLY the problem is/what you want to archieve.
 
Last edited:
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
OK but I couldn't figure out how to implement his suggestion. And sorry i don't know how to use the code tags.

1. Use code tags when posting code!
2. Why are you creating a new thread for your problem?
3. @Troberg already gave you the solution. So. After you read the list check for emtpy lines - and delete them if there are any - before you want to sort it.

Most probably it is better to post your problematic project (In the ide File-Export as ZIP) and upload this zip here describing what EXACTLY the problem is/what you want to archieve.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Log("*************************************") 
    Dim test As List
    test.Initialize2(Array As String("","Test","2","3","",""))
    Log("*********** initial content *********") 
    Log(test)
    test.Sort(True)
    Log("*********** sorted ASC      *********") 
    Log(test)
    test = filteremptylines(test)
    Log("*********** filtered        *********") 
    Log(test)
    Log("*************************************") 



Sub filteremptylines(source As List) As List
    Dim ret As List
    ret.Initialize
    If source.Size > 0 Then
        For i = 0 To source.Size-1
            If source.Get(i) <> "" Then
                ret.Add(source.Get(i))
            End If
        Next
    End If
    Return ret
End Sub

*********** initial content *********
(ArrayList) [, Test, 2, 3, , ]
*********** sorted ASC *********
(ArrayList) [, , , 2, 3, Test]
*********** filtered *********
(ArrayList) [2, 3, Test]
*************************************
 
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
B4X:
    Log("*************************************")
    Dim test As List
    test.Initialize2(Array As String("","Test","2","3","",""))
    Log("*********** initial content *********")
    Log(test)
    test.Sort(True)
    Log("*********** sorted ASC      *********")
    Log(test)
    test = filteremptylines(test)
    Log("*********** filtered        *********")
    Log(test)
    Log("*************************************")



Sub filteremptylines(source As List) As List
    Dim ret As List
    ret.Initialize
    If source.Size > 0 Then
        For i = 0 To source.Size-1
            If source.Get(i) <> "" Then
                ret.Add(source.Get(i))
            End If
        Next
    End If
    Return ret
End Sub


Thanks very much for your help!
 
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
Interestingly if I use List.Sort to sort a list items in a text file in alphabetical order it works correctly but appends a blank line to the end of the file.
Please see the attached zip file. The file to be sorted (Txt.txt) should be in the DirDefaultExternal directory.
 

Attachments

  • ListSort.zip
    340.9 KB · Views: 142
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
List.Sort doesn't add items. You can put a breakpoint and see that there are 6 items after the call to List.Sort:
SS-2015-04-21_07.52.52.png


File.WriteList writes each item in a line and adds a new line character. This is the source for the extra line. Note that File.ReadList will still return 6 items.

Ok thanks Erel. I think I can proceed from here.
 
Upvote 0
Top