Solved

Sort arraylist

Posted on 2015-01-30
5
159 Views
Last Modified: 2016-02-17
I have an arraylist

Untitled.png
I need to sort array by the number before the comma

I need it with lowest to highest
0
Comment
Question by:Jason
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40579562
I would suggest to split up the values into a small helper class, and compare using the proper parts with the propert data types. which means to sort by "part(6)" from the source data

trying to sort like this after the string concatenation is not really efficient...

hence, can you not sort the "input" instead?
0
 

Author Comment

by:Jason
ID: 40579596
If i separate them how do you keep the association between them
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40579623
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40580216
Why are you using an ArrayList?...they are deprecated and hard to work with.

It can be done, however:
        Dim list As New ArrayList
        list.Add("10.90,1")
        list.Add("7.50,2")
        list.Add("20.40,3")
        list.Add("4.40,4")
        list.Add("11.70,5")
        list.Add("10.80,6")
        list.Add("48.00,7")
        list.Add("2.50,8")
        list.Add("12.80,9")

        list = New ArrayList(list.ToArray.Select(Function(x) x.ToString).ToList.OrderBy(Function(x) CDbl(x.Split(",")(0))).ToArray)
        For Each x In list
            Debug.Print(x)
        Next

Open in new window


If you can't use the fancy LINQ stuff, then you'll have to go with Guy's suggestion.

Preferably, though, you should be using a List and have those values in some kind of structure (like a Class); then you could sort it quite easily.
0
 

Author Closing Comment

by:Jason
ID: 40580872
Great works awesome
Only using arraylist because it is extracted from another arraylist
will need to look at LINQ.

Thx heaps
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question