0

to set the scene, I have a list of objects. Each object contains a name, and a point consisting of an x coordinate and a y coordinate.

I currently have the list sorted to group up the object's name in alphabetical order. Now I want the same named objects to be ordered by descending or ascending x coordinates.

  • What is inside the list now:

    • BP301 (5, 6)
    • BP301 (-3, 2)
    • BP301 (0, 4)
    • BP301 (5, 6)
    • BP302 (10, 7)
    • BP302 (10, 7)
    • GP001 (8, 3)
  • What I want inside the list:

    • BP301 (-3, 2)
    • BP301 (0, 4)
    • BP301 (5, 6)
    • BP301 (5, 6)
    • BP302 (10, 7)
    • BP302 (10, 7)
    • GP001 (8, 3)

Hopefully my question is clear enough

1
  • 1
    Can you show the code you are using now to store and sort these objects? Commented Jul 2, 2017 at 21:31

1 Answer 1

2

Have you considered using LINQ?

Rough example given no code, so I'm assuming an IEnumerable<T> compatible list:

var sortedList = list.OrderBy(i => i.Name).ThenBy(i => i.X).ThenBy(i => i.Y);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that worked, thank you. I need to look more into LINQ. I'm bad with list sorting.
Yeh there's a bunch of fun stuff you can do, sort ascending, sort descending, group and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.