Solved

Sort arraylist

Posted on 2015-01-30
5
154 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]
Comment Utility
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
Comment Utility
If i separate them how do you keep the association between them
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
Great works awesome
Only using arraylist because it is extracted from another arraylist
will need to look at LINQ.

Thx heaps
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now