Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Updates and Questions
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Updates and Questions Updates and questions regarding Basic4android.

Sorting an Array

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2011, 04:39 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default Sorting an Array

HI all...It's me again

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?
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!
Reply With Quote
  #2 (permalink)  
Old 01-31-2011, 05:23 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Cableguy View Post
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.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.

Last edited by agraham : 01-31-2011 at 05:38 PM.
Reply With Quote
  #3 (permalink)  
Old 01-31-2011, 05:42 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

Thanks Andrew, I will give it a run...
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!
Reply With Quote
  #4 (permalink)  
Old 02-03-2012, 10:30 AM
Junior Member
 
Join Date: Dec 2011
Posts: 19
Default 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.
Reply With Quote
  #5 (permalink)  
Old 02-04-2012, 02:45 AM
warwound's Avatar
Basic4ppc Expert
 
Join Date: Jun 2011
Location: Norfolk, UK
Posts: 675
Lightbulb

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.
Reply With Quote
  #6 (permalink)  
Old 02-04-2012, 12:19 PM
Junior Member
 
Join Date: Dec 2011
Posts: 19
Default

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.

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!
Reply With Quote
  #7 (permalink)  
Old 02-04-2012, 12:23 PM
warwound's Avatar
Basic4ppc Expert
 
Join Date: Jun 2011
Location: Norfolk, UK
Posts: 675
Lightbulb

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

Martin.
Reply With Quote
  #8 (permalink)  
Old 02-04-2012, 12:48 PM
Junior Member
 
Join Date: Aug 2011
Location: Poitou Charente, France
Posts: 49
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting arrays derez Basic4android Updates and Questions 0 01-17-2011 09:33 AM
Sorting A 2 Dimensional Array ceaser Questions (Windows Mobile) 7 03-23-2010 08:06 PM
SQLCreateTable and sorting N1c0_ds Questions (Windows Mobile) 4 02-21-2009 04:48 AM
sqlite sorting dennishea Questions (Windows Mobile) 3 09-12-2008 07:26 PM
Sql sorting problem... belotrei Questions (Windows Mobile) 4 06-05-2007 11:26 AM


All times are GMT. The time now is 10:50 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0