1

For example:

Array
ID | Primary | Data2
------------------
1  | N       | Something 1
2  | N       | Something 2
3  | Y       | Something 3

I'm trying to sort it based on the primary column and I want the "Y" to show first. It should bring all the other column at the top.

The end result would be:

Sorted Array
ID | Primary | Data2
------------------
3  | Y       | Something 3
1  | N       | Something 1
2  | N       | Something 2

Is there a pre-made function for that. If not, how do we do this?

It is declared like this:

Dim Array(,) As String

regards,

Johnny Prescott
  • 263
  • 6
  • 23

2 Answers2

1

I like using LINQ's OrderBy and ThenBy to order collections of objects. You just pass in a selector function to use to order the collections. For example:

orderedObjs = objs.OrderByDescending(function(x) x.isPrimary).ThenBy(function(x) x.id).ToList()

This code orders a collection first by the .isPrimary boolean, then by the id. Finally, it immediately evaluates the query into a List and assigns it to some variable.

Demo

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
  • Working like a charm. Had to tweak it a bit to make it work. This is a great way to accomplish this. I have to modify the logic in the software cause i was not using object before. It is better that way anyway. – Johnny Prescott Jul 13 '15 at 19:20
0

There's a similar C# question whose solution applies just as well to VB. In short, you can use an overload of Array.Sort if you first split your 2D array into separate (1D) arrays:

Dim Primary() As String
Dim Data2() As String
// ...
Array.Sort(Primary,Data2)

This would reorder Data2 according to the Y/N sort of Primary, after which point you could then recombine them into a 2D array.

Community
  • 1
  • 1