473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sorting by multiple properties

Good afternoon,

I would like to mimic the sorting capabilities in Excel where I can sort a
list by up to four properties (columns in excel). I would like to pass a
collection of objects to a function and get a sorted collection back. The
sorting would occur based on the user's selection of up to four properties of
the objects in the collection. The sorting may be requested based on UP TO
four properties but could just as easily happen for one property.

How can I code this functionality? What is this type of sorting called so I
can research an algorithm? Does this code already exist somewhere and I can
borrow it?

Thanks,
Scott

Nov 3 '08 #1
3 3209

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!141.entry

I'm assuming you mean "In DotNet".

........

PS You can read the followup comments for other possibles...however, I
would not recommend any reflection based solutions.
(That's my opinion, not a "fact").

...........


"Scott" <Sc***@discussions.microsoft.comwrote in message
news:A3**********************************@microsof t.com...
Good afternoon,

I would like to mimic the sorting capabilities in Excel where I can sort a
list by up to four properties (columns in excel). I would like to pass a
collection of objects to a function and get a sorted collection back. The
sorting would occur based on the user's selection of up to four properties
of
the objects in the collection. The sorting may be requested based on UP TO
four properties but could just as easily happen for one property.

How can I code this functionality? What is this type of sorting called so
I
can research an algorithm? Does this code already exist somewhere and I
can
borrow it?

Thanks,
Scott

Nov 3 '08 #2
Scott wrote:
Good afternoon,

I would like to mimic the sorting capabilities in Excel where I can sort a
list by up to four properties (columns in excel). I would like to pass a
collection of objects to a function and get a sorted collection back. The
sorting would occur based on the user's selection of up to four properties of
the objects in the collection. The sorting may be requested based on UP TO
four properties but could just as easily happen for one property.

How can I code this functionality? What is this type of sorting called so I
can research an algorithm? Does this code already exist somewhere and I can
borrow it?

Thanks,
Scott
You could make an IComparer for each property, and make them chainable
so that you can combine them in any way you like.

Something like:

Public Class NameComparer
Implements IComparer(Of SomeClass)

Private _next As IComparer(Of SomeClass)

Public Sub NameComparer(next as IComparer(Of SomeClass))
_next = next
End Sub

Public Function Compare(x As SomeClass, y As SomeClass) As Integer
Dim result As Integer = String.Compare(x.Name, y.Name)
' Check if names are equal and if there is a next comparer
If result = 0 AndAlso _next Is Not Nothing Then
' Use the next comparer
result = _next.Compare(x, y)
End If
Return result
End Function

End Class

Now you can create comparers and chain them to sort the list on for
example name, city and phone number (building backwards):

Dim comparer As IComparer = New PhoneComparer(Nothing)
comparer = New CityComparer(comparer)
comparer = New NameComparer(comparer)
theListToSort.Sort(comparer)

--
Göran Andersson
_____
http://www.guffa.com
Nov 4 '08 #3
Scott,

As you compare this with an excel sheet, then I assume you are talking about
a kind of data.

In a datatable you can sort in any direction you want, like the same you can
do in Linq for if fact everything.

Cor

"Scott" <Sc***@discussions.microsoft.comschreef in bericht
news:A3**********************************@microsof t.com...
Good afternoon,

I would like to mimic the sorting capabilities in Excel where I can sort a
list by up to four properties (columns in excel). I would like to pass a
collection of objects to a function and get a sorted collection back. The
sorting would occur based on the user's selection of up to four properties
of
the objects in the collection. The sorting may be requested based on UP TO
four properties but could just as easily happen for one property.

How can I code this functionality? What is this type of sorting called so
I
can research an algorithm? Does this code already exist somewhere and I
can
borrow it?

Thanks,
Scott

Nov 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim::.. | last post by:
Hi, Can someone tell me how I sort a datagrid when using ActiveDirectory as my Datasource! I would like to be able to add sorting to my datagrid but don't know how to achive it! Thanks for...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
7
by: Peter Kirk | last post by:
Hi say I have a list of Person objects, which I want to sort. But I don't just want to sort by "name" say, but by several fields in the Person object. Maybe 1st priority by height, 2nd priority...
0
by: Roy | last post by:
Hey all, I must be losing my touch. I have made many pages in the 1.1 framework that utilize custom bidirectional paging in datagrids. We've converted over to 2.0 and I've been trying to use the...
2
by: Daz | last post by:
Hi everyone. I am trying to make a program which handles about 70 different objects, of the same type. The properties are as follows: std::string ItemName int ItemType int Calories int...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: kstevens | last post by:
I have a report, and on the report is a min, avg, and max query each with (ok who really cares) 20 or so records. At the footer of the report i would like to have a total. I requery for each...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.