Question Advanced sorting problem

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Hi, having a bit of a sorting problem here, hoping some kind sould can help me with it

Situation: I have a class with three properties:
A (integer), B (string), C (integer)

I have added a list of these items in random sequence to a list:

A B C
3 Bacon 1
1 Doom 3
2 Even 2
1 Abe 1
2 Zebra 1
1 True 2

At first it has to group them together on property A:

1 Doom 3
1 Abe 1
1 True 2
2 Even 2
2 Zebra 1
3 Bacon 1

The next part is sorting on property C, still keep them in a group:

1 Abe 1
1 True 2
1 Doom 3
2 Zebra 1
2 Even 2
3 Bacon 1


The last part is sorting on property B for groups. In the above situation group 1 is using "Abe" for sorting, group 2 is using "Zebra" and group 3 is using "Bacon". This way group 3 should be after group 1, but before group 2:

1 Abe 1
1 True 2
1 Doom 3
3 Bacon 1
2 Zebra 1
2 Even 2


Would it be possible to sort like this?

In my class I have Implemented IComparable, but I can't seem to figure out what I would need to write in CompareTo. Or do I need to take another aproach to sorting this list?
 
Why is the syntax backwards?
The fact that it's not exactly the same as SQL doesn't make it backwards. A lot of people believe that that's the way SQL should be too as it's more natural. It can be a little confusing at first because we're so used to SQL but you get over it pretty quickly. All the other clauses are in basically the same order. It's just that the Select clause comes last instead of first.

I wasn't resistant to LINQ at all and I knew the generalities but it took me quite a while to actually try it out for myself. Now that I've used a bit though I use it everywhere I can. I don't mean that I go looking for places to use it but any time I need to sort, filter, group, aggregate or project I ask myself whether LINQ can make my life easier and it usually can. It often removes the need for a For or For Each loop.

There are also many situations where you don't necessarily need LINQ itself but you can benefit by using extension methods that were created for LINQ, e.g. instead of:
VB.NET:
For Each ctl As Control In Me.Controls
    If TypeOf ctl Is Button Then
        With DirectCast(ctl, Button)

        End With
    End If
Next
you can use:
VB.NET:
For Each btn As Button In Me.Controls.OfType(Of Button)()

Next
Note that this is LINQ to Objects, which is only one aspect of LINQ support as well.
 
The fact that it's not exactly the same as SQL doesn't make it backwards. A lot of people believe that that's the way SQL should be too as it's more natural.

It depends on how you speak I suppose. I would say get "get me a chocolate bar from the shop" not "from the shop, get me a chocolate bar"
 
It depends on how you speak I suppose. I would say get "get me a chocolate bar from the shop" not "from the shop, get me a chocolate bar"
The FROM clause in a LINQ query declares the variable and the SELECT clause uses it. If the SELECT clause preceded the FROM clause then you'd be using a variable before you declared it. The IDE could support this if it was told to but it's not intuitive, given how VB works. This would be a unique situation where a variable could be used before being declared.

There are various other reasons that it was done that way and there has been plenty written about it on the Web. SELECT\FROM seems natural to a lot of people because SQL does it that way and SQL is the only query syntax that many people have used. It's not the only one that exists though, and several others do not put the SELECT clause first.

Quite frankly, it really doesn't matter either way. Although I knew there were technical reasons for it, it did seem awkward to me at first too. Now that I'm used to it, it's just the way it is and it really is irrelevant. One is no more backwards compared to the other than VB or C# variable declarations are. They are the opposite way around but neither is right or wrong and we just use whichever the current language dictates.
 
It depends on how you speak I suppose. I would say get "get me a chocolate bar from the shop" not "from the shop, get me a chocolate bar"
One more thing worth noting: the VB team were originally going to use the SQL-style SELECT/FROM syntax because it was familiar to SQL users and because it read much more like an English sentence, a la your example, which is the norm for VB. C# was always going to use FROM/SELECT, mostly for more technical reasons. Presumably the technical reasons became more important to the VB team and they switched to FROM/SELECT before the final release.
 
Back
Top