Sorting an Array

Cableguy

Expert
Licensed User
Longtime User
HI all...It's me again :sign0188:

I am strugling to get my mind around a concept on how to sort an array...
Since B4A still hasn't 2D arrays, I used a continuous one...
So for sake of argument, lets use this example:

Score(4) = 23
Score(9) = 25
Score(14) = 21
Score(19) = 30
Score(24) = 22

The values in beetween are relevant, but not to the sorting...
So my question is how can I rearrange my array in order to get as the lowest index level, the highest value?
 

agraham

Expert
Licensed User
Longtime User
Since B4A still hasn't 2D arrays
Actually it does. You can have as many dimensions as you need, or at least as many as your brain can cope with.

Dim AnArray(1,2,3 ...) As Whatever


For sorting use a List. You can add an array directly to a List.

Dim A() As Int
A = Array As Int(5, 7, 1, 2, 9, 2)
Dim L As List
L.Initialize
L.AddAll(A)
L.Sort(True)

You can actually use an array anywhere where a List can be used as Basic4android will convert it for you, but not unfortunately the other way round. I don't think there is a quick way to convert from a List to an array except by doing it item by item within a For loop.

EDIT :- Corrected the stupid use of Add instead of AddAll in the original code fragment.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks Andrew, I will give it a run...
 
Upvote 0

cbal03

Member
Licensed User
Longtime User
how do I sort a list?

I have a list. Each item in the list is comprised of a set of delimited values. Let's say these values are a string "name,age,height". How would I sort the list by age? If I say list.sort(true) then the list is sorted by name only.

The only way I can see how to do this is to shift the values into another list so that age is first then do the sort but then again I'm new to B4A so hopefully there is a more efficient way as my list will grow into tens of thousands.

Thanks.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Have you thought about using a database to hold your values?

A column for each value: name, age and height.

You can then SELECT your values and use ORDER BY on any column to get your data in whatever format and order you want.

Martin.
 
Upvote 0

cbal03

Member
Licensed User
Longtime User
Thank you Martin.
I would gladly use that approach but I'm having troubles with repeated http requests. After 200 or so requests I get OOM errors.

If a user clicks a column header then I would have to re-request the 'sorted' data (php does all of the work interacting with the database) and rebuild the grid accordingly.
This is all simple enough but the OOM issue is driving me crazy.

Since I need to make lots of requests in other areas it's probably going to be worth tracking the OOM problem. Once that is solved I'll use your suggestion most eagerly. :sign0098:

I wrote a very simple test tool to make repeated requests. Using this tool I am working to eliminate any memory leaks in my code. I'm not finding any though :(

At this point I'm unsure if the memory leak is caused by the quantity of data returned and associated object handling or if it being caused by the HTTP library.

Sorry I didn't elaborate on my initial question.

I'll post a question about http requests in the appropriate forum.


Thank you very much!
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You'll probably find that uploading your project will help someone to track down the memory leaks.

Martin.
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
cbal03,
depending how big and/or dynamic the data set is you could possibly download the entire database to a SQLite database on the device then do all the selection and sorting locally. One bigger download at the start vs 200 or so smaller downloads may prove more efficient.
I have an application with some 20,000 rows across 5 tables and never fail to be impressed with the performance of SQLite on quite lowly powered Android devices when making quite complex joins.

Cheers,
Douglas
 
Upvote 0
Top