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

Discussion in 'Android Questions' started by TonyVerberne, Apr 18, 2015.

  1. TonyVerberne

    TonyVerberne Member Licensed 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)
    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
  2. DonManfred

    DonManfred Expert Licensed 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: Apr 18, 2015
    lemonisdead and LucaMs like this.
  3. TonyVerberne

    TonyVerberne Member Licensed 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.

  4. DonManfred

    DonManfred Expert Licensed User


  5. DonManfred

    DonManfred Expert Licensed User

    Dim test As List
    Array As String("","Test","2","3","",""))
    Log("*********** initial content *********"
    Log("*********** sorted ASC      *********"
        test = filteremptylines(test)
    Log("*********** filtered        *********"

    Sub filteremptylines(source As ListAs List
    Dim ret As List
    If source.Size > 0 Then
    For i = 0 To source.Size-1
    If source.Get(i) <> "" Then
    End If
    End If
    Return ret
    End Sub
    TonyVerberne, lemonisdead and LucaMs like this.
  6. TonyVerberne

    TonyVerberne Member Licensed User

    Thanks very much for your help!
  7. TonyVerberne

    TonyVerberne Member Licensed 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.

    Attached Files:

  8. Erel

    Erel Administrator Staff Member Licensed 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:

    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.
    TonyVerberne likes this.
  9. TonyVerberne

    TonyVerberne Member Licensed User

    Ok thanks Erel. I think I can proceed from here.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice