Author Topic: Double comparison  (Read 176 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 120
Double comparison
« on: June 29, 2017, 09:57:28 am »
I would like to sort a list base on two values.  Is it possible to add another compare in my class BlkFleche ?
I need the list to be sort base on the X and then base on the Y, those are from coordinates. 

So if I have my X and Y like that :
[10,1]
[4,6]
[9,2]
[10,4]
[4,2]
[10,6]

I would like the end result to be :
[4,2]
[4,6]
[9,2]
[10,1]
[10,4]
[10,6]

I'm using a list because I need to keep data in relation with the coordinates.

Code - C#: [Select]
  1. public class blkFleche : IComparable<blkFleche>
  2. {
  3.    public double positionX { get; set; }
  4.    public double positionY { get; set; }
  5.    public string attNOCABLE { get; set; }
  6.    public string attNOFIBRE { get; set; }
  7.  
  8.    public int CompareTo(blkFleche other)
  9.    {
  10.        return this.positionX.CompareTo(other.positionX);
  11.    }
  12. }
  13.  
  14. private void btnRead()
  15. {
  16.      List<blkFleche> listBlkSort = new List<blkFleche>();
  17.      //...
  18.      blkFleche blkNew = new blkFleche();
  19.      //...
  20.      blkNew.positionX = blkRef.Position.X;
  21.      blkNew.positionY = blkRef.Position.Y;
  22.      //...
  23.      listBlkSort.Add(blkNew);
  24.      //...
  25.      listBlkSort.Sort();
  26. }

Jeff H

  • Needs a day job
  • Posts: 5853
Re: Double comparison
« Reply #1 on: June 29, 2017, 10:11:27 am »
I might have a chance later to create an example, but LINQ has ThenBy that you could use.
https://msdn.microsoft.com/en-us/library/bb534743(v=vs.110).aspx

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: Double comparison
« Reply #2 on: June 29, 2017, 10:57:53 am »
Hi,

You can implement IComparable<blkFleche> this way:

Code - C#: [Select]
  1.    public class blkFleche : IComparable<blkFleche>
  2.    {
  3.        public double positionX { get; set; }
  4.        public double positionY { get; set; }
  5.        public string attNOCABLE { get; set; }
  6.        public string attNOFIBRE { get; set; }
  7.  
  8.        public int CompareTo(blkFleche other)
  9.        {
  10.            int compareX = this.positionX.CompareTo(other.positionX);
  11.            return compareX == 0 ? this.positionY.CompareTo(other.positionY) : compareX;
  12.        }
  13.    }

But as Jeff said, you can directly use linq extension methods:

Code - C#: [Select]
  1. List<blkFleche> listBlk = new List<blkFleche>();
  2.  
  3. // fill the listBlk
  4.  
  5. List<blkFleche> listBlkSort = listBlk
  6.    .OrderBy(blk => blk.positionX)
  7.    .ThenBy(blk => blk.positionY)
  8.    .ToList();

Or, more efficiently, if you do not absolutely need a List<blkFleche>:

Code - C#: [Select]
  1. IEnumerable<blkFleche> listBlkSort = listBlk
  2.    .OrderBy(blk => blk.positionX)
  3.    .ThenBy(blk => blk.positionY);
Speaking English as a French Frog

latour_g

  • Newt
  • Posts: 120
Re: Double comparison
« Reply #3 on: June 29, 2017, 04:14:42 pm »
Great, thank you both !