How to Sort a List of Objects by dictionary value?
How would I sort this by a chosen dictionary value in Person?
Example: Order List of "Person" by "cars" value descending.
Is that possible with one of those fancy lambda/linq equations?
public class People
{
List<Person> Persons { get; set; }
}
public class Person
{
public Dictionary<string, string> cars { get; set; }
public Dictionary<string, string> houses { get; set; }
public Dictionary<string, string> banks { get; set; }
}
................
People people = new People();
people.Persons = new List<Person>
{
new Person
{
cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } },
houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
banks = new Dictionary<string, string> { { "swiss", "12500330000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } },
houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } },
houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
}
};
EDIT / EXAMPLE RESULT:
The result would be a new or reordered list based on a value contained in the dictionary of each Person
Person -> cars -> { "volvo", "34023200" }
Person -> cars -> { "volvo", "3554000" } Person -> cars -> { "volvo", "340050" }The new or reordered List would look exactly like this:
people.Persons = new List<Person>
{
new Person
{
cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } },
houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } },
houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
},
new Person
{
cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } },
houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
banks = new Dictionary<string, string> { { "swiss", "开发者_Python百科12500330000" }, { "camen", "12000000" } }
}
};
Here's a nifty trick; basically, a Dictionary can be thought of as a List of KeyValuePair<> objects.
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
foreach(var item in myDictionary)
// item is a KeyValuePair<int, string>, enumerated in order of their insertion into the dictionary
Now that it is in a list, it's easy to sort via LINQ:
Dictionary<int, string> sample = new Dictionary<int, string>
{
{ 1, "Sample" },
{ 2, "Another Sample" }
};
var orderedList = sample.OrderBy(x => x.Value).ToList();
Of course, the bigger problem is why you would need to sort and order a Dictionary - that's not exactly an operation that is really grouped with the concept of a dictionary (it's more for quick access of specific elements via a specified key). The problem description you write about seems odd; you can order the cars for a specific person, but are you trying to find each car from each person ordered by value?
That's easy to do as well:
List<PersonCarDetails> allStuff = new List<PersonCarDetails>();
foreach(var person in persons)
allStuff.AddRange(person.Cars.Select(x => new PersonCarDetails { PersonId = person.Id, CarName = x.Key, CarId = x.Value }).ToList());
// allStuff now contains a list of PersonCarDetails, and then you can order that:
var orderedList = allStuff.OrderBy(x => x.CarId).ToList();
If you mean sort by the number of cars owned by the person
var result = Person.OrderByDescending(x => x.cars.Count);
If you mean sort by the first (sorted by name) car name owned by the person
var result = Person.OrderByDescending(x => x.cars.Keys.OrderBy(y => y.Key).FirstOrDefault());
If you mean sort by the first (sorted by number) car number owned by the person
var result = Person.OrderByDescending(x => x.cars.Keys.OrderBy(y => y.value).FirstOrDefault());
An example output would be good to clarify what actually you want to sort by, since cars is a list, rather than just an element.
Based on the examples you provided this might just do the job for you:
var result = Person.OrderByDescending(x => x.cars.Values.FirstOrDefault());
精彩评论