1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c# how do i sort a group of lists

Discussion in 'HTML, Graphics & Programming' started by dal, Jun 22, 2018.

  1. dal

    Hitman

    Joined: Sep 10, 2005

    Posts: 702

    Location: Lincolnshire

    Hi all. I know this may not be the best way of doing this but I've got a small group of lists that represent aircraft data.
    Which looks like this :

    public List <string> AIRCRAFT_ID = new List<string>();
    public List <int> ALTITUDE = new List<int>();
    public List <int> passengers_on_board = new List<int> ();

    So each element of the 3 lists represents one aircraft. This works fine when all I'm doing is adding aircraft to the list.

    Now how would I get on if i wanted to sort the list based on say altitude because if I was to do something like :
    ALTITUDE.sort .....

    Then the other lists would be out of sync.

    This is quite new to me so hopefully any help would be in laymans terms (if possible).

    Regards
     
  2. Quartz

    Soldato

    Joined: Apr 1, 2014

    Posts: 5,468

    Location: Aberdeen

    You need one array with three items in each record.
     
  3. billysielu

    Sgarrista

    Joined: Aug 9, 2009

    Posts: 9,468

    Location: Oxfordshire

  4. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,887

    Class and linq.

    public class Aircraft
    {
    public string AIRCRAFT_ID { get; set; }
    public int ALTITUDE { get; set; }
    public int passengers { get; set; }
    }

    var aircrafts = new List<Aircraft>();

    aircrafts.Add(new Aircraft { AIRCRAFT_ID = "y", ALTITUDE = 1, passengers = 100 });
    aircrafts.Add(new Aircraft { AIRCRAFT_ID = "a", ALTITUDE = 2, passengers = 200 });
    aircrafts.Add(new Aircraft { AIRCRAFT_ID = "z", ALTITUDE = 2, passengers = 400 });

    aircrafts = aircrafts.OrderBy(x => x.AIRCRAFT_ID).ToList();

    Can also sort on multiple items.

    aircrafts = aircrafts.OrderBy(x => x.AIRCRAFT_ID).ThenBy(x => x.passengers).ToList();
     
  5. dal

    Hitman

    Joined: Sep 10, 2005

    Posts: 702

    Location: Lincolnshire

    cheers Ill take a look
     
  6. dal

    Hitman

    Joined: Sep 10, 2005

    Posts: 702

    Location: Lincolnshire

    I've decided to go with this but how could I get the index of a certain record if I only had say the aircraft ID ?
    Regards.
     
  7. Spunkey

    Capodecina

    Joined: Oct 18, 2002

    Posts: 13,140

    Location: The land of milk & beans

    Use FindIndex()

    Code:
    aircrafts.FindIndex(a => a.AIRCRAFT_ID == "y");
     
  8. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,887

    You can also just grab the whole class object with linq and access the relevant data that way.

    var x = aircrafts.FirstOrDefault(a => a.AIRCRAFT_ID == "y");

    can then do
    x.ALTITUDE
    or
    x.passengers


    also sort your variable names out :p
     
  9. Spunkey

    Capodecina

    Joined: Oct 18, 2002

    Posts: 13,140

    Location: The land of milk & beans

    100% this. If you worked in my office you'd already have been yelled at :)
     
  10. dal

    Hitman

    Joined: Sep 10, 2005

    Posts: 702

    Location: Lincolnshire

     
  11. dal

    Hitman

    Joined: Sep 10, 2005

    Posts: 702

    Location: Lincolnshire

    Lol cheers guys