Solved

Sort arraylist

Posted on 2015-01-30
5
158 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 142

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 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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