william007
william007

Reputation: 18535

How to sort List according to value in an array

I have

List<string> strs;
double[] values;

where the values array contains the value of each of the string in strs list

Say strs={"abc","def","ghi"}
and values={3,1,2}

this means "abc" has value 3 and so on.

I wish to sort strs and values ordered by values, such that it becomes

strs={"def","ghi","abc"}
values={1,2,3}

Is there any easy way to achieve this?

Upvotes: 0

Views: 244

Answers (6)

YoryeNathan
YoryeNathan

Reputation: 14512

var sortedStrs = strs.Select((i, s) => new {Value = values[i], Str = s})
                     .OrderBy(x => x.Value)
                     .Select(x => x.Str).ToList();

If you could logically put those values as properties of a class, such as:

class NameAndOrder
{
    public string Name;
    public int Order;
}

Then it would be better and more organized, and then you could do:

var items = new List<NameAndOrder>(strs.Count);

for (var i = 0; i < strs.Count; i++)
{
    items.Add(new NameAndOrder { Name = strs[i], Order = values[i] });
}

items.Sort((a, b) => a.Order.CompareTo(b.Order));

Upvotes: 2

Guffa
Guffa

Reputation: 700272

The Array.Sort method has an overload that takes two arrays and sorts both arrays according to the values in the first array, so make an array out of the list:

string[] strsArr = strs.ToArray();

Then sorting them can't be simpler:

Array.Sort(values, strsArr);

And then back to a list, if you need that:

strs = strsArr.ToList();

Upvotes: 2

L.B
L.B

Reputation: 116108

var strs = new[] { "abc", "def", "ghi" };
var values = new[] { 3, 1, 2 };

var newArr = strs.Select((s, i) => new { s, i })
                 .OrderBy(x => values[x.i])
                 .Select(x => x.s)
                 .ToArray();

Upvotes: 1

Leo
Leo

Reputation: 109

Why Don't you use Dictionary Object..

Dictionary<string, int> dictionary =
    new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
// Acquire keys and sort them.
var list = dictionary.Keys.ToList();
list.Sort();

Upvotes: 1

Connell
Connell

Reputation: 14411

How are you setting up these collections? Or are you given these two parameters?

You could create a StringAndOrder class and use LINQ:

public class StringAndOrder
{
    public string String { get; set; }
    public double Order { get; set; }
}

List<StringAndOrder> list; //create with this structure instead
var orderedStrings = list.OrderBy(item => item.Order).Select(item => item.String);

Upvotes: 2

to StackOverflow
to StackOverflow

Reputation: 124696

You can use Enumerable.Zip, then sort the result, then extract the list of strings.

Something like:

var result = strs.Zip(values, (first, second) => new Tuple<string, double>(first, second))
            .OrderBy(x => x.Item2)
            .Select(x => x.Item1)
            .ToList();

Upvotes: 2

Related Questions