How to Sort an Array of Objects?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • regalis
    New Member
    • Mar 2010
    • 9

    How to Sort an Array of Objects?

    Hi everybody,
    i'm new to this forum and also to C#.
    I hope someone with more experience can help me with my problem.
    Given is the following Class
    Code:
            public class movie
            {
                public long ID { get; set; }
                public string NAME { get; set; } // 0
                public string SIZE { get; set; } // 1
                public string TYPE { get; set; } // 2
                public DateTime CHANGEDATE { get; set; } // 3
                public DateTime CREATIONDATE { get; set; } // 4
                public DateTime LASTUSED { get; set; } // 5
                public string ARCHIVE { get; set; }
                public string PATH { get; set; }
    
                public movie() { }
            }
    I have an Array of movie Objects, now i wanted to sort the Array by "ARCHIVE" which contains a string like "HDD 1" or "DVD - 145".

    The sorting is quite good with this line of code:
    Code:
    Array.Sort(MovieArray, (x, y) => string.Compare(x.ARCHIVE, y.ARCHIVE, true));
    But the Problem is that when all Movies have the same ARCHIVE-String (because they are all on the same Medium) the Names of the Movies will be totaly unsorted after sorting by ARCHIVE.
    It looks like this:

    Code:
    [B]Before sorting:[/B]
    ["Movie A", "HDD1"]
    ["Movie B", "HDD1"]
    ["Movie C", "HDD1"]
    ["Movie D", "HDD1"]
    ["Movie E", "HDD1"]
    
    [B]After Sorting:[/B]
    ["Movie D", "HDD1"]
    ["Movie E", "HDD1"]
    ["Movie A", "HDD1"]
    ["Movie B", "HDD1"]
    ["Movie C", "HDD1"]
    Does anyone have a hint how to sort first by ARCHIVE and after that by NAME?
    What i generally want is:
    Code:
    ["Movie A", "HDD1"]
    ["Movie B", "HDD1"]
    ["Movie C", "HDD1"]
    ["Movie D", "HDD1"]
    ["Movie E", "HDD1"]
    ["Movie A", "HDD2"]
    ["Movie B", "HDD2"]
    ["Movie C", "HDD2"]
    ["Movie D", "HDD2"]
    ["Movie E", "HDD2"]
    ["Movie A", "HDD4"]
    ["Movie B", "HDD4"]
    ["Movie C", "HDD4"]
    ["Movie D", "HDD4"]
    ["Movie E", "HDD4"]
    Thank you for reading my Post and hopefully you can help me with this.

    greetings regalis
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Hy,

    if you're able to use LINQ, you can do it that way:

    Code:
    //MovieArray has type movie[]
    movie[] ordered1 = MovieArray.OrderBy(mov => mov.ARCHIVE).ThenBy(mov => mov.NAME).ToArray();
    just the same, only in other notation:

    Code:
    movie[] ordered2 = (from mov in MovieArray
                        orderby mov.ARCHIVE, mov.NAME
                        select mov).ToArray();

    Comment

    • regalis
      New Member
      • Mar 2010
      • 9

      #3
      Hi ChBinder,
      thank you very much for your help.
      I like the way you have done the sorting and it works perfect!
      100000 hugs to you :)

      greetz regalis

      Comment

      Working...