Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sorting Arraylists Twice

Sorting Arraylists Twice

Scheduled Pinned Locked Moved C#
algorithmsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jgallen23
    wrote on last edited by
    #1

    I have a class called tasks and right now it uses compareto to sort by duedate when it is in an arraylist, that is fine for right now, but I'm expanding to group by category. I now want to sort by category and then sort by duedate within the category sort. Is this possible? Here's my class: public class Task: IComparable { private int _importance; public int Importance { get { return _importance; } set { _importance = value; } } private DateTime _DueDate; public DateTime DueDate { get { return _DueDate; } set { _DueDate = value; } } private string _subject; public string Subject { get { return _subject; } set { _subject = value; } } private string _entryID; public string EntryID { get { return _entryID; } set { _entryID = value; } } private string _category; public string Category { get { return _category; } set { _category = value; } } public int CompareTo(object other) { return _DueDate.CompareTo(((Task)other)._DueDate); } }

    S 1 Reply Last reply
    0
    • J jgallen23

      I have a class called tasks and right now it uses compareto to sort by duedate when it is in an arraylist, that is fine for right now, but I'm expanding to group by category. I now want to sort by category and then sort by duedate within the category sort. Is this possible? Here's my class: public class Task: IComparable { private int _importance; public int Importance { get { return _importance; } set { _importance = value; } } private DateTime _DueDate; public DateTime DueDate { get { return _DueDate; } set { _DueDate = value; } } private string _subject; public string Subject { get { return _subject; } set { _subject = value; } } private string _entryID; public string EntryID { get { return _entryID; } set { _entryID = value; } } private string _category; public string Category { get { return _category; } set { _category = value; } } public int CompareTo(object other) { return _DueDate.CompareTo(((Task)other)._DueDate); } }

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Try this:

      public int CompareTo(object value)
      {
      Task compObj = (Task) value;
      int result = _category.CompareTo(compObj._category);
      // Check if instances don't differ in their Category.
      if (result == 0)
      result = _DueDate.CompareTo(compObj._DueDate);
      return result;
      }


      www.troschuetz.de

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups