3

I have an array of strings such as "blue", "green", "red" and I wish to sort them so the longest string comes first and the shortest last.

Currently I am creating another array with the lengths of each string in the array in the same index positions and using this array as the key array to sort by as can be seen below, but I think this could be optimised into one line perhaps?

Dim colours() As string = {"blue", "green", "red"}
Dim colourslength() As Integer
For i As Integer = 0 To colours.Length - 1
 colourslength(i) = colours(i).Length
Next
Array.Sort(colourslength, colours)
Array.Reverse(colours)

Edit: just realised I defined colours as a list in the example code, it's an array in my actual code.

2
  • 1
    @davisoa: I would guess .net 4 as that inline list initialization isn't valid in earlier versions. Commented Apr 4, 2012 at 15:45
  • it is .net 4, I used a list by accident though, sorry about that. Commented Apr 4, 2012 at 15:54

4 Answers 4

5

Another Linq solution (warning, converted from C#)

Dim Sorted = From p In colours Order By p.Length Descending Select p
Sign up to request clarification or add additional context in comments.

1 Comment

Dim q = From el In colours Order By el.Length Descending. Select is not compulsory in VB.NET. If omitted the element of the selection will be the element type )).
3

To my opinion this is the shortes way. Use linq.

Dim strs = New String() {"", "333", "22", "4444", "55555", "1"}
Dim sorted = strs.OrderBy(Function(x) x.Length).ThenBy(Function(x) x).ToArray()

Edit

If you want a reverse order just get rid of extra method call an do the sortin in a reverse order

Dim strs = New String() {"", "333", "22", "4444", "55555", "1"}
Dim sorted = strs.OrderByDescending(Function(x) x.Length).ThenByDescending(Function(x) x).ToArray

Cheers.

3 Comments

The OrderBy needs to be OrderByDescending as he wants longest first.
Could you explain what the second part of this line does? Dim sorted = strs.OrderByDescending(Function(x) x.Length).ThenByDescending(Function(x) x).ToArray the ThenByDescending(Function(x) x) part. I'm reading it that the first part of that line does the actual sorting?
@MarcusLars, OrderByDescending(Function(x) x.Length) order by length. ThenBy is applied when there are strings with equal length. Two equal length strings will be sorted alphabetically.
3
Dim colours = {"blue", "green", "red"}
Dim coloursSortedByLength = colours.OrderByDescending(Function(c) c.Length)

Output sequence is: green, blue, red.

Comments

2

The best simple way to do it, is to compare every string with all other strings in your list:

in Java:

for(int i=0; i<list.length-1; i++){
    for(int j=i+1; j<list.length; j++){
        if(list[i].length() < list[j].length()){
            tmp = list[i];
            list[i] = list[j];
            list[j] = tmp;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.