Sorting List<int> low to high, then sorting other lists based on that

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Sorting List<int> low to high, then sorting other lists based on that

    I cannot seem to even think of a way I might do this. Basically I have 6 lists with various pieces of information about the data I'm processing. I need to sort one of the lists (the one containing the date/time information) and have the rest of the lists sort the same exact way.

    So let's say I have these 2 lists:

    List<int> dates = new List<int>() { 56789, 01234 };
    List<string> names = new List<string>() { name1, name2 };

    I sort the "dates" list so that it's ordered from lowest to highest:

    01234
    56789

    Then the names list needs to adjust accordingly, meaning it would sort to:

    name2
    name1

    How would I go about doing something like this?

    In PHP there are associative arrays that made this simple, but with this many lists it'd still be a huge pain.

    Any help will be most appreciated, I can't really continue with my program until I sort this out.
  • fastrizwaan
    New Member
    • May 2010
    • 1

    #2
    try dictionary/hashes with "list" as dictionary.item s, in other words nest your lists into a dictonary/hash object. then sorting all lists become very easy. or nest dictionary inside dictionary. sorry, i don't know c# but I would nest dictionaries in python3.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      I ended up creating a new class to hold all my information:

      Code:
      public class screenshotItem
      {
          public string fileName { get; set; }
          public string shotTitle { get; set; }
          public int gameID { get; set; }
          public int jpegOffset { get; set; }
          public int jpegLength { get; set; }
          public int fileTime { get; set; }
      }
      Then I made 1 list:

      List<screenshot Item> screenshots = new List<screenshot Item>();

      And to add to that list:

      Code:
      screenshotItem screenshot = new screenshotItem() {
          fileName = finfo.FullName,
          shotTitle = shotTitle,
          gameID = gameID,
          jpegOffset = jpegOffset,
          jpegLength = jpegLength,
          fileTime = fileTime};
      screenshots.Add(screenshot);
      Now to sort:

      Code:
      IEnumerable<screenshotItem> sortedScreenshots =
          from screenshot in screenshots
          orderby screenshot.fileTime
          select screenshot;
      This way beats having 6 individual Lists, then the hacked together code I was using to be able to display all the items, etc. It's much cleaner and I can sort it perfectly.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Originally posted by HaLo2FrEeEk
        I ended up creating a new class to hold all my information:

        Code:
        public class screenshotItem
        {
            public string fileName { get; set; }
            public string shotTitle { get; set; }
            public int gameID { get; set; }
            public int jpegOffset { get; set; }
            public int jpegLength { get; set; }
            public int fileTime { get; set; }
        }
        Then I made 1 list:

        List<screenshot Item> screenshots = new List<screenshot Item>();

        And to add to that list:

        Code:
        screenshotItem screenshot = new screenshotItem() {
            fileName = finfo.FullName,
            shotTitle = shotTitle,
            gameID = gameID,
            jpegOffset = jpegOffset,
            jpegLength = jpegLength,
            fileTime = fileTime};
        screenshots.Add(screenshot);
        Now to sort:

        Code:
        IEnumerable<screenshotItem> sortedScreenshots =
            from screenshot in screenshots
            orderby screenshot.fileTime
            select screenshot;
        This way beats having 6 individual Lists, then the hacked together code I was using to be able to display all the items, etc. It's much cleaner and I can sort it perfectly.
        A quicker version of this (with a more usable result) is this:
        Code:
        List<screenshotItem> sorted = screenshots.OrderBy(x => x.fileTime).ToList();
        You do it all on one line, skip the select, and end up with a list instead of an IEnumerable (lists are easier to work with, all though all IEnumerables support the ToList()).

        Comment

        • HaLo2FrEeEk
          Contributor
          • Feb 2007
          • 404

          #5
          Thanks, I'll look into that, but I'd still display the items the same way:

          Code:
          foreach (screenshotItem shot in sortedScreenshots)
          {
              string fileName = shot.fileName.Remove(0, shot.fileName.LastIndexOf(@"\") + 1);
              ListViewItem lvi = new ListViewItem(fileName);
              lvi.SubItems.Add(shot.shotTitle);
              lvi.SubItems.Add(gamesTitles[shot.gameID]);
              lvi.SubItems.Add("0x" + shot.jpegOffset.ToString("X"));
              lvi.SubItems.Add(shot.jpegLength.ToString());
              lvi.SubItems.Add(fatXDate(shot.fileTime).ToString());
              fileList.Items.Add(lvi);
          }
          It would be nice to have it on 1 line though.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Another way I've seen to do this is to have inherit the class from IComparable, eg:
            Code:
            public class screenshotItem : IComparable
            {
             ... 
                     public int CompareTo(Object o)
                    {
                        if (o.GetType() != typeof(screenshotItem))
                          return 0;
                        return fileTime - ((screenshotItem)o).fileTime;
                    }
            Then the List.Sort() methods can still be used as before.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Originally posted by jkmyoung
              Another way I've seen to do this is to have inherit the class from IComparable, eg:
              Code:
              public class screenshotItem : IComparable
              {
               ... 
                       public int CompareTo(Object o)
                      {
                          if (o.GetType() != typeof(screenshotItem))
                            return 0;
                          return fileTime - ((screenshotItem)o).fileTime;
                      }
              Then the List.Sort() methods can still be used as before.
              Right. My version is basically taking one property of the object to sort by and using the built in comparison. If you wanted to use my way to sort the list based on an custom object property, they would also have to implement IComparable.

              Comment

              Working...