Android Question sort list that contain a map

Devv

Active Member
Licensed User
Longtime User
the title says it all
i have a list that contains maps
how can i sort the lsit ?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
My point was that a map can contain many pieces of data, you would need to know which piece of data you want to sort it by then write a routine specifically to do it. As Erel suggested, the easiest way would be to create a custom type which holds the map, and a sort field, or all of the data instead of a map. You can then use the standard List SortType methods.
 
Last edited:
Upvote 0

Devv

Active Member
Licensed User
Longtime User
Hi all

i tried to do a custom type
B4X:
    For i = 0 To sources_map.Size -1 ' Loop through sources
           
            JSON.Initialize(File.ReadString(File.DirInternal,sources_map.GetKeyAt(i) & ".json"))
            JSON_root = JSON.NextObject
            JSON_rss = JSON_root.Get("rss")
            JSON_channel = JSON_rss.Get("channel")
            JSON_items = JSON_channel.Get("item")
           
            For x = 0 To JSON_items.Size -1 ' loop through the source items and add them to feeds list
                JSON_item = JSON_items.Get(x) 'the map 
        
               
                Dim feeds_list_item As Advanced_JSON ' custom type
                feeds_list_item.pupdate = DateTime.DateParse(JSON_item.Get("pubDate")) ' the thing that i want to sort the list with
                feeds_list_item.JSON_item = JSON_item ' the actual data field
                feeds.Add(feeds_list_item) ' the list
                Msgbox(feeds,"")
            Next
        Next

the result of the msgbox is on item repeated for all the list !
why is that happening ?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Because you didn't dimmed JSON_item in the loop.
' dim JSON_item as map = JSON_items.Get(x) 'the map
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
Because you didn't dimmed JSON_item in the loop.
' dim JSON_item as map = JSON_items.Get(x) 'the map
Woow that worked , but i cant understand why
why it did not worked when i dimmed JSON_item in the Globals !?
why should i dim it inside the loop even that i'm changing its value in each loop cycle

JSON_item = JSON_items.Get(x)

please explain to me
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
why should i dim it inside the loop even that i'm changing its value in each loop cycle
learn about objects and their references...
You are using ONE Instance of the type. and you always change this instance. You list hold references to this objects. As you are only using one ionstance only one is listed.
You need to create a new instance in the loop.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
When you add an item to the list you're actually adding a reference to it not its value.
So when you code a loop as you did, you end up with a list of the same reference repeated "n" times and this causes its last value to be displayed for each item.
Dimming at each cycle will create a new reference, so each item will sport a different reference, each "pointing" to a different value.

Look for parameter reference vs value in the forum for a deeper explanation from Erel.

ps: this time Manfred was quicker than me..eheh
 
Upvote 0
Top