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. Multiple Field Sorting C#

Multiple Field Sorting C#

Scheduled Pinned Locked Moved C#
csharpalgorithmsjson
7 Posts 4 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.
  • D Offline
    D Offline
    dsrao
    wrote on last edited by
    #1

    Hi All, I want to sort collections.. // Class public class Item { public String Name{get;set;} public int Cost{get;set;} public Item(string name,int cost) { Name=name; Cost=cost; } } // Collection of Data List items=new List(); items.Add(New Item("House",100)); items.Add(New Item("Car",80)); items.Add(New Item("Dog",10)); items.Add(New Item("Car",50)); items.Add(New Item("Cycle",25)); items.Add(New Item("Car",120)); Note: Provide code to sort a collection of Item. The code should sort the Item collection only one time, following this rule: The Items should be ordered by their names (following the standard C# API for string ordering), but if two Items have the same name, then those Items should be ordered by their cost, with the lower cost item to appear before the higher cost item. I appreciate somebody provide solution for the same.

    Thanks & Regards Rao

    U R 2 Replies Last reply
    0
    • D dsrao

      Hi All, I want to sort collections.. // Class public class Item { public String Name{get;set;} public int Cost{get;set;} public Item(string name,int cost) { Name=name; Cost=cost; } } // Collection of Data List items=new List(); items.Add(New Item("House",100)); items.Add(New Item("Car",80)); items.Add(New Item("Dog",10)); items.Add(New Item("Car",50)); items.Add(New Item("Cycle",25)); items.Add(New Item("Car",120)); Note: Provide code to sort a collection of Item. The code should sort the Item collection only one time, following this rule: The Items should be ordered by their names (following the standard C# API for string ordering), but if two Items have the same name, then those Items should be ordered by their cost, with the lower cost item to appear before the higher cost item. I appreciate somebody provide solution for the same.

      Thanks & Regards Rao

      U Offline
      U Offline
      Uri Lavi
      wrote on last edited by
      #2
              IEnumerable<Item> sortedItems = from i in items
                                              orderby i.Name, i.Cost
                                              select i;
      

      Also look here[^], for more complex examples.

      Uri

      D 1 Reply Last reply
      0
      • D dsrao

        Hi All, I want to sort collections.. // Class public class Item { public String Name{get;set;} public int Cost{get;set;} public Item(string name,int cost) { Name=name; Cost=cost; } } // Collection of Data List items=new List(); items.Add(New Item("House",100)); items.Add(New Item("Car",80)); items.Add(New Item("Dog",10)); items.Add(New Item("Car",50)); items.Add(New Item("Cycle",25)); items.Add(New Item("Car",120)); Note: Provide code to sort a collection of Item. The code should sort the Item collection only one time, following this rule: The Items should be ordered by their names (following the standard C# API for string ordering), but if two Items have the same name, then those Items should be ordered by their cost, with the lower cost item to appear before the higher cost item. I appreciate somebody provide solution for the same.

        Thanks & Regards Rao

        R Offline
        R Offline
        riced
        wrote on last edited by
        #3

        Use this

        public class Item : IComparable
        {

        public String Name{get;set;}

        public int Cost{get;set;}
        public Item(string name,int cost)
        {
        Name=name;
        Cost=cost;
        }

        int Icomparable.CompareTo(object obj)
        {
        Item other = (Item) obj;
        If this.Name < other.Name return -1;
        If this.Name > other.Name return 1;
        If this.Cost > other.Cost return 1;
        If this.Cost < other.Cost return -1;
        return 0;
        }
        }

        Then you can just do items.Sort() Probably worth having a look at IComparable and IComparer interfaces. You could alsi look at using List in generics namespace.

        Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

        L 1 Reply Last reply
        0
        • R riced

          Use this

          public class Item : IComparable
          {

          public String Name{get;set;}

          public int Cost{get;set;}
          public Item(string name,int cost)
          {
          Name=name;
          Cost=cost;
          }

          int Icomparable.CompareTo(object obj)
          {
          Item other = (Item) obj;
          If this.Name < other.Name return -1;
          If this.Name > other.Name return 1;
          If this.Cost > other.Cost return 1;
          If this.Cost < other.Cost return -1;
          return 0;
          }
          }

          Then you can just do items.Sort() Probably worth having a look at IComparable and IComparer interfaces. You could alsi look at using List in generics namespace.

          Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          riced wrote:

          Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;

          Item other = (Item) obj;
          int diff=string.Compare(this.Name, other.Name);
          if (diff==0) diff=this.Cost-other.Cost;
          return diff;

          And using a generic IComparable, you wouldn't need the cast. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          R D 2 Replies Last reply
          0
          • L Luc Pattyn

            riced wrote:

            Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;

            Item other = (Item) obj;
            int diff=string.Compare(this.Name, other.Name);
            if (diff==0) diff=this.Cost-other.Cost;
            return diff;

            And using a generic IComparable, you wouldn't need the cast. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            That's much neater. :) Looking at the OPs code I think they are a beginner, that's why I did it like I did and suggested looking at the interfaces.

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

            1 Reply Last reply
            0
            • U Uri Lavi
                      IEnumerable<Item> sortedItems = from i in items
                                                      orderby i.Name, i.Cost
                                                      select i;
              

              Also look here[^], for more complex examples.

              Uri

              D Offline
              D Offline
              dsrao
              wrote on last edited by
              #6

              Thanks...

              Thanks & Regards Rao

              1 Reply Last reply
              0
              • L Luc Pattyn

                riced wrote:

                Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;

                Item other = (Item) obj;
                int diff=string.Compare(this.Name, other.Name);
                if (diff==0) diff=this.Cost-other.Cost;
                return diff;

                And using a generic IComparable, you wouldn't need the cast. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                D Offline
                D Offline
                dsrao
                wrote on last edited by
                #7

                Thanks..

                Thanks & Regards Rao

                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