Android Question Tableview sorting problem

ilan

Expert
Licensed User
Longtime User
hi

i like to work with the tableview, i am using it to fill with data and then transfer what i need to a clv.
i am not viewing the table on my app, only filling it with data and using it like a LISTVIEW (VB listview item with some subitems)

now my problem is that, when i want to sort the tableview.
i have a col with dates in it like (1/1/2013,...)
and if i sort the tableview it sort it not correctly, like this:

i have 5 dates:
1/1/2013
5/1/2013
11/1/2013
15/1/2013
2/1/2013

after tabel1.quicksort
i get:

1/1/2013
11/1/2013
15/1/2013
2/1/2013
5/1/2013

how can i sort correctly?? please help me !!
thanks
 

ilan

Expert
Licensed User
Longtime User
Is there a quicksort method in TableView?

sorry i meant table1.sort :)

thank you thats solved my problem

can you tell me please what is the different between table1.selectionsort and table1.sort?

thanx
 
Upvote 0

Sathish VM

Member
Licensed User
Longtime User
I'm also using SelectionSort In Table class and I've hit a similar issue when sorting numbers. Numbers are sorted as strings and this results in incorrect sorting.I've explained the issue here too. Is there any quick way to resolve this problem?

Or do I have to write to melmoud?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
after i used yyyy-MM-dd format my problem was solved..

if you are using dates you should try this is you are using numbers 1,2,3.. and you get 1,11,12,..2,20,... than you should format to 2 numbers
01,02,03,...11,12,...20,21...

and the sorting will be correct
 
Upvote 0

Sathish VM

Member
Licensed User
Longtime User
if you are using dates you should try this is you are using numbers 1,2,3.. and you get 1,11,12,..2,20,... than you should format to 2 numbers
01,02,03,...11,12,...20,21... and the sorting will be correct

This partly solved the problem but the negative numbers are still messed up. -18 is smaller than -10, but as strings it shows otherwise. Is there any workaround for handling negative numbers?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
sort the negative numbers seperatly, create another table and add the seond table all items with positiv numbers >=0 and leave the table1 only with negative numbers, then sort table1 = dec and table 2 = acc and now add all items from table2 to table 1 and you will get the right order

B4X:
Sub Activity_Create(FirstTime As Boolean)

'create second tableview
Dim table2 As Table
table2.Initialize(Me, "Table1", 5 ,Gravity.CENTER,True)
table2.AddToActivity(Activity, 200%x, 0dip, 100%x, 50%y)'the x is set bigger than the activity wight because i dont want to see the table view, its just for sorting
table2.clearall
table2.SetHeader(Array As String("Col1", "Col2", "Col3", "Col4","Col5"))

end sub


Sub sorter

Table2.ClearAll

Table1.sortTable(4,True) 'first sort the table1 with all his items negative and positive

Dim lastpos As Int 'want to start from the last item
lastpos = (Table1.Size - 1)

For i = 0 To Table1.Size - 1

    If Table1.GetValue(4,lastpos - i) >= 0 Then
    table2.AddRow(Array As String(Table1.GetValue(0,i),Table1.GetValue(1,i),Table1.GetValue(2,i),Table1.GetValue(3,i),Table1.GetValue(4,i)))' add all positive numbers to table2
    Table1.RemoveRow(lastpos - i)
    End If

Next

'now we have all negative numbers in table1 and all positive in table2

Table1.sortTable(4,False)'sort dec
table2.sorttable(4,True)

For i = 0 To table2.Size - 1
Table1.AddRow(Array As String(table2.GetValue(0,i),table2.GetValue(1,i),table2.GetValue(2,i),table2.GetValue(3,i),table2.GetValue(4,i)))
Next



End Sub
 
Upvote 0

Sathish VM

Member
Licensed User
Longtime User
B4X:
Sub sorter

End Sub

This is a good idea, thanks for your suggestion.

My question is: Should I call this sorter sub in the Header_Click function of the table? I think No, right? Within the table class, there is no need to refer to Table1.Size etc, so I guess I've to keep this in the main program. If its in the main program, where will I call it from?

Is there any event that gets triggered on header_click that I can over-ride in main program, call this sorter and then call the Table1.Header_click manually? Is this the right thing to do?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
call it when you add items to table1 you would like to get them sorted when you add them right?
or you want them to be sorted only if you export them??

cal the sub when you like to sort the tableview, like you said when you click the header
 
Last edited:
Upvote 0

Sathish VM

Member
Licensed User
Longtime User
call it when you add items to table1 you would like to get them sorted when you add them right?
or you want them to be sorted only if you export them??

cal the sub when you like to sort the tableview, like you said when you click the header
I dont want to sort when adding items. I want to sort when user clicks on header. I dont know where to place this sorter. Should this be in table class or main program?
 
Last edited:
Upvote 0

Sathish VM

Member
Licensed User
Longtime User
is it working i have not test my code :rolleyes:

The logic seems to be working, but there are some mistakes in your code, the biggest one in the below line. We've to add the row which gives table1.getvalue(0,lastpos-i).

B4X:
table2.AddRow(Array As String(Table1.GetValue(0,i),Table1.GetValue(1,i),Table1.GetValue(2,i),Table1.GetValue(3,i),Table1.GetValue(4,i)))' add all positive numbers to table2

Feedback on sorting:

It takes too much time for the sort to complete and the complete table to display.. about 15 seconds. I've 206 rows in the table.
I think some rows are disappearing. I haven't tested in rigorously to see what rows and why they are vanishing, I'll give you an update after couple of days.

I think there has to be an easier and quicker way to sort. I guess I'll sort in the SQL table using query and just display results, rather than using two tables and doing a sort in the UI. It seems to be bad for performance.
 
Upvote 0

GGS

Member
Licensed User
Longtime User
I too could use a simpler way to sort numbers correctly. Right now I have taken the sort out of the UI, let the DB do it, then clear the table and display the results again.
 
Upvote 0
Top