How do I sort by number

jschuchert

Active Member
Licensed User
Longtime User
My application uses a multi-line text box in which I feed a row of numbers from a comma-delimited text file like in the following code:
B4X:
Sub btnListOK_Click
Select txtlistDec.Text 
   Case 0
   sdecpl="N0"
   Case 1
   sdecpl="N1"
   Case 2
   sdecpl="N2"
   Case 3
   sdecpl="N3"
   Case 4
   sdecpl="N4"
   Case 5
   sdecpl="N5"
 End Select
FileClose(c)
FileOpen(c,strfilename,cRead)
nextline:
lineoftext=FileRead(c)
If lineoftext="" Then Goto listend
coord()=StrSplit(lineoftext,",")
txtlistdisplay.Text =txtlistdisplay.text & coord(0) & " " & Format(coord(1),sdecpl) & " " & _
Format(coord(2),sdecpl) & " " & coord(3) & CRLF
Goto nextline
FileClose(c)
listend:
End Sub

The rows of numbers may look like this in the text file:
1, 1205.678, 2345.123, iron_rod
12, 3456.789, 9876.234, None
4, 1234.567, 2145.587, None

The comma is replaced with a space in the display text box

The first number is not always consecutive to the one before it. However, when I display it to the user, I would like to have the rows sorted on the first number.

I saw a "Sort" function in the "Constants" in the docs but don't know how to incorporate it if that is even possible. I could also use a list box if that would make it easier to sort. Thanks for any advice.

Jim Schuchert
 

Ariel_Z

Active Member
Licensed User
Try this:
B4X:
Sub btnListOK_Click
   Select txtlistDec.Text 
   Case 0
         sdecpl="N0"
   Case 1
         sdecpl="N1"
   Case 2
      sdecpl="N2"
   Case 3
         sdecpl="N3"
   Case 4
         sdecpl="N4"
   Case 5
         sdecpl="N5"
   End Select
   
   FileClose(c)
   FileOpen(c,strfilename,cRead)
   
   AddComboBox("Form1", "Combo1", 0, 0, 70, 22)
   AddArrayList("Arr")
   AddHashtable("Hash")
   lineoftext = FileRead(c)
   Do While lineoftext <> EOF
      coord()=StrSplit(lineoftext,",")
      ErrorLabel(textInputErrorLabel)
      strText = coord(0) & " " 
      strText = strText & Format(coord(1),sdecpl) & " " 
      strText = strText & Format(coord(2),sdecpl) & " " 
      strText = strText & coord(3) & CRLF
      textInputErrorLabel:
      Arr.Add(coord(0))
      Hash.Add(coord(0), strText)
      lineoftext=FileRead(c)
   Loop
   
   ErrorLabel(ExitErrorLabel)
   Arr.Sort(cNumbers)
   For I = 0 To Arr.Count - 1
      Combo1.Add(Hash.Item(Arr.Item(I)))
   Next
   
   ExitErrorLabel:
   FileClose(c)
End Sub

It uses a hash table to store pairs of key and value, and an array list to sort the keys. Note that I totally changed the way you wrote it usign "goto". It's usually not recommended to use goto when you can avoid it.
 

jschuchert

Active Member
Licensed User
Longtime User
Ariel,

Again I must thank you for your help. I never would have figured that one out. Incidentally, I had used "do until lineoftext = eof" but it gave me a "index outside of the bounds of the array' error message. After studying your code, I saw my problem and corrected it. I will now work on the sort. Thanks again.

Jim
 

jschuchert

Active Member
Licensed User
Longtime User
Hi Ariel,

I copied and pasted your code into a new form, changed the name of the form to what I am using and executed. Nothing happened. Nothing was put into the combo box and I could only stop it by 'Debug-Stop' rather than clicking the 'x' on the form. I must be doing something wrong or expecting results that require further action on my part. Will the combo box expand as data is added or does it require using the drop-down arrow. I was hoping that more than 1 line of data would be shown as in a list or text box with a scroll bar.

Jim
 

Zenerdiode

Active Member
Licensed User
...And you will figure these out after you've seen enogh code. Don't worry. Just keep on .

I like that, Ariel

At first I used to feel really awkward when asking questions that may have looked simple to the more experienced users - I'd never heard of Select & Case before I started with Basic4PPC for goodness sake - but now I know that you may ask anything; and if someone knows the solution, they will offer it freely.
 

Ariel_Z

Active Member
Licensed User
jschuchert, Sorry - I've just seen you post.
1. I guess you did something like placing it on the Form_Show event and then calling Form.Show - these kind of things (the Form_Show event will then be recursively called) might halt the app. Could you upload the source code as zip?
2. You may use a listview instead of the combobox, this way you will have it all placed in a nice list. Play on a seperate program with both and see the differences...
 

jschuchert

Active Member
Licensed User
Longtime User

Ariel,

It works perfectly now after I corrected MY code. You really nailed it with yours. Great job. I also changed the font to a "7" in order for the entire string to show since a listbox doesn't appear to 'word wrap'. I know I am not using the listbox the way it was intended. Now I will study your code to understand it. I am familiar with nsbasic, embedded basic and visual basic but each one is a little different, especially the syntax. I really like b4ppc now and think it has much potential for my application. One of the things I have not been able to figure out is how to make the newest calculation result (sent to a textbox) scroll to the top. The way it is now, the scroll bar moves 'up', thereby placing the recent calculation result at the bottom, not visible in most cases because of the size of the textbox. I could do it with selstart, sellength, etc. in other software but not with b4ppc. I'm confident it can be done but I haven't found the method yet. It isn't critical.

Thanks again, Ariel.

Jim
 

Ariel_Z

Active Member
Licensed User
You are welcome... They are really a bit different - mainly because of the different underlying technologies.

ListBox actually does not "word wrap". That's right. But I think if you used it instead of the combo, then it is the way it is supposed to, isn't it?
And as for the textbox, try TextBox.ScrollToCaret.
 

jschuchert

Active Member
Licensed User
Longtime User
Thanks, Ariel, I had already tried "scrolltocaret' but it didn't work. Here is an excerpt of the code I used:
B4X:
strscreendisplay = sFrompoint & "  " & main.strBear & _
"  " & strdistance & "  " & _
Format(dblNorth, sdecpl) & "  " & _
Format(dblEast, sdecpl) & "  " & _
strDes & "  " & sTopoint

main.txtembedDisplay.Text  = main.txtembedDisplay.Text  & strscreendisplay & CRLF
'the next line is supposed to allow the text display to scroll
[color=blue]main.txtembedDisplay.ScrollToCaret[/color]   'doesn't do what I want

Maybe it isn't supposed to be executed from a sub outside of the module. It's not that important right now so don't spend any time on it. I will address it in the manual.

Jim
 

Ariel_Z

Active Member
Licensed User
You are right. I forgot you should place TextBox.SelectionStart = StrLength(TextBox.Text) before calling ScrollToCaret. This code works:
B4X:
Sub App_Start
   Form1.Show
   TextBox1.Text = TextBox1.Text & "dfdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddxxxxxl"
   TextBox1.Text = TextBox1.Text & "dfdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddl"
   TextBox1.Text = TextBox1.Text & "dfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddxxxl"
   TextBox1.Text = TextBox1.Text & "dfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddxxxxxx"
   
   Textbox1.SelectionStart = StrLength(Textbox1.Text)
   Textbox1.ScrollToCaret
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…