Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
GeneralRe: ALEX POST IT HERE PLEASE!!!! Pin
Christian Graus30-Jun-09 18:35
protectorChristian Graus30-Jun-09 18:35 
GeneralRe: ALEX POST IT HERE PLEASE!!!! Pin
dan!sh 30-Jun-09 19:24
professional dan!sh 30-Jun-09 19:24 
GeneralRe: ALEX POST IT HERE PLEASE!!!! Pin
Rajesh R Subramanian30-Jun-09 21:08
professionalRajesh R Subramanian30-Jun-09 21:08 
GeneralRe: ALEX POST IT HERE PLEASE!!!! Pin
Pete O'Hanlon30-Jun-09 21:31
mvePete O'Hanlon30-Jun-09 21:31 
JokeRe: ALEX POST IT HERE PLEASE!!!! Pin
kstls30-Jun-09 22:58
kstls30-Jun-09 22:58 
QuestionSorting by various fields Pin
Quake2Player30-Jun-09 15:12
Quake2Player30-Jun-09 15:12 
AnswerRe: Sorting by various fields Pin
Christian Graus30-Jun-09 18:36
protectorChristian Graus30-Jun-09 18:36 
AnswerRe: Sorting by various fields Pin
dojohansen30-Jun-09 23:56
dojohansen30-Jun-09 23:56 
There are, as you say, many solutions to this. As always it comes down to making the right tradeoffs (between encapsulation, performance, coding effort, ...) and there is really no single answer that is "the best way" for all scenarios.

As long as the field types are IComparable you could implement a "ListSorter" class that could sort any List<T> by any field using reflection. This would require relatively little code and the great benefit is that you do it once and then sort whatever object types you want to. The downside is that it's slow, but that may not be a problem if the collections are small. An interesting possibility is to build on this approach by making a ListSorterFactory class instead; this could use reflection to dynamically generate a ListSorter class for a specific type. It may sound like a very advanced sort of thing, but .NET has actually made this kind of thing relatively easy to do.

If you have just one type of collection (and don't mind that this code won't be reusable anytime in the future - a sorter factory is potentially useful in many projects down the line) you can always go for inheriting from List<T> and add sort methods using the built-in Sort function with a predicate that compares the particular field the method sorts by. Since this requires the least code, I'll provide an example, but personally I think the ListSorterFactory is a rather more interesting idea!

public class Item
{
  public int Number;
  public DateTime Time;
  public string Message;
}

public class ItemCollection : List<Item>
{
    public void SortByNumber()
    {
        Sort((Item a, Item b) => a.Number.CompareTo(b.Number));  
    }

    public void SortByTime()
    {
        Sort((Item a, Item b) => a.Time.CompareTo(b.Time));
    }

    public void SortByMessage()
    {
        Sort((Item a, Item b) => a.Message.CompareTo(b.Message));
    }
}


However, since the pattern of this code is so regular, why not just generate it instead of writing it manually? Using reflection you can easily find all public fields or properties (or non-public if that's of interest, though you shouldn't do this with private members in any case!) and generating the code is then completely straightforward.

And now I realize that dynamically compiling such generated code may not be so meaningful, because you've got no obvious way to *use* that generated code in your non-generated code! (If there was a single interface with many implementations generated code would do the trick, but here the generated ListSorter<T> has an interface that itself depends on T.)

Hope this helps. Wink | ;)
Questionhow to generate a mobile agent from sms?? Pin
razmeenia30-Jun-09 13:35
razmeenia30-Jun-09 13:35 
QuestionProblem to call method from a WebSercive... Pin
Mtyb30-Jun-09 12:51
Mtyb30-Jun-09 12:51 
AnswerRe: Problem to call method from a WebSercive... Pin
Christian Graus30-Jun-09 13:28
protectorChristian Graus30-Jun-09 13:28 
GeneralRe: Problem to call method from a WebSercive... Pin
Mtyb30-Jun-09 14:14
Mtyb30-Jun-09 14:14 
GeneralRe: Problem to call method from a WebSercive... Pin
Christian Graus30-Jun-09 14:38
protectorChristian Graus30-Jun-09 14:38 
GeneralRe: Problem to call method from a WebSercive... Pin
Mtyb30-Jun-09 14:44
Mtyb30-Jun-09 14:44 
GeneralRe: Problem to call method from a WebSercive... Pin
Christian Graus30-Jun-09 14:54
protectorChristian Graus30-Jun-09 14:54 
Questionabout Visual studio 2010 beta 1 Pin
Seraph_summer30-Jun-09 10:47
Seraph_summer30-Jun-09 10:47 
AnswerRe: about Visual studio 2010 beta 1 Pin
Abhijit Jana30-Jun-09 10:53
professionalAbhijit Jana30-Jun-09 10:53 
AnswerRe: about Visual studio 2010 beta 1 Pin
Christian Graus30-Jun-09 11:12
protectorChristian Graus30-Jun-09 11:12 
GeneralRe: about Visual studio 2010 beta 1 Pin
DaveyM6930-Jun-09 12:11
professionalDaveyM6930-Jun-09 12:11 
GeneralRe: about Visual studio 2010 beta 1 Pin
Christian Graus30-Jun-09 13:28
protectorChristian Graus30-Jun-09 13:28 
GeneralRe: about Visual studio 2010 beta 1 Pin
Seraph_summer30-Jun-09 23:35
Seraph_summer30-Jun-09 23:35 
GeneralRe: about Visual studio 2010 beta 1 Pin
Dan Neely1-Jul-09 10:59
Dan Neely1-Jul-09 10:59 
Questioncurve from bezier cusps Pin
googlejumbo30-Jun-09 10:11
googlejumbo30-Jun-09 10:11 
AnswerRe: curve from bezier cusps Pin
molesworth30-Jun-09 22:09
molesworth30-Jun-09 22:09 
GeneralRe: curve from bezier cusps Pin
googlejumbo1-Jul-09 7:12
googlejumbo1-Jul-09 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.