0

Say I have a list of cars:

List<string> carList = new List<string>(){"Car1", "Car2", "Car3"};

I then have prices assigned to each car in another list where the first item corresponds to Car1 etc:

List<int> priceList = new List<int>(){50, 200, 10};

Now I want to sort my carList based on the highest price in priceList, with the expected outcome of my carList now having the order like this:

"Car2", "Car1", "Car3"

This needs to be done as fast as possible, it is the only thing that matters. It doesn't matter if carList is sorted itself or if I need to create a new list to store the sorted values in. I don't have any use of priceList after this sorting is completed and don't need its values.

5
  • so what did you try yourself already? SO isn't a code-writing-service whom you can give a task "give me the code". You have to provide some own affords. Commented Feb 18, 2022 at 10:18
  • 1
    anyway, why don't you just create a Car-class with properties like "Name" and "Price"? Then it's pretty easy to have a list of those instances and sort that. Commented Feb 18, 2022 at 10:20
  • I could probably provide some loop which checks each value and sort them very inefficiently but that wouldn't really help you? I am trying to find what the most efficient way of doing it is. Commented Feb 18, 2022 at 10:21
  • In this case I can't have a class of items, the preconditions are given in the question. Is it possible to do what I ask about? Commented Feb 18, 2022 at 10:21
  • 1
    "sort them very inefficiently" Well, we cannot help you much, as we simply have no clue if "our" solution is in any way better than "your" one. Furthermore "Fastest" highly depends on your data: how much data is it and how often do you change that data, e.g. add items to both lists or remove them? Commented Feb 18, 2022 at 10:25

3 Answers 3

1

You can benefit from Linq's .Zip() to associate price with car name, then order by price, then select the car names.

List<string> sortedCars = carList
    .Zip(priceList,
        ( car, price ) => ( Name: car, Price: price))
    .OrderByDescending(car => car.Price)
    .Select(car => car.Name)
    .ToList();

Example fiddle here;

Sign up to request clarification or add additional context in comments.

2 Comments

This works great thank you! Is it fast though?
You're welcome! I don't know whether it's faster than the other suggested approaches, unfortunately.
0

Having the price on other List only related by the 'id' of both of them is a handicap, so I go for a intermediate List with both values, then sort it and cast it again to the original class list, a little unefficient, but you have little options to start with

List<string> cartests = carList.Select(
        (item, index) => new cartest() 
           { carName= item, carPrice = priceList[index] })
    .OrderByDescending(o=> o.carPrice )
    .Select(itemo=> itemo.carName).ToList();

Intermediate class

    private class cartest 
    { 
        public string carName{ get; set; }
        public int carPrice { get; set; }
    }

Comments

0

If you can use arrays instead of lists, you can use Array.Sort(Array, Array) to sort two arrays at the same time.

Otherwise you will have to convert your lists to an array, sort the arrays, and then convert back:

List<string> carList = new List<string> { "Car1", "Car2", "Car3" };
List<int> priceList = new List<int> { 50, 200, 10 };

var carArray = carList.ToArray();

Array.Sort(priceList.ToArray(), carArray, Comparer<int>.Create((a, b) => b.CompareTo(a)));

carList = carArray.ToList();

The Comparer<int>.Create() is to reverse the sort order.

Comments

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.