C # sorting a list of strings by a list of pairs

I need to create a list of rows that represent the first five of the summed values.

I have a database with hundreds of accounts related to different services.

ex. electricity $ 600 January 2013 Water $ 50 January 2013

I need to summarize all the same services that I did here

public List<Double> GetSumOfSingleServices { get { var sums = (from dc in GetDashboardData group dc by dc.serviceType into g select g.Sum(sc => sc.serviceCost)).ToList(); return sums; } set { NotifyPropertyChanged("GetSumOfSingleServices"); } } 

I created a list of lines with the following code

 public List<String> GetServiceNames { get { var names = (from dc in GetDashboardData group dc by dc.serviceType into g select g.First().serviceType).ToList(); return names; } set { NotifyPropertyChanged("GetServiceNames"); } } 

Now the data in these two lists is parallel to GetSumOfSingleServices [0] - this is the value for GetServiceNames [0], etc.

I would like to have a list in which Strings are ranked by the highest value from GetSumOfSingleServices, etc.

So, if the highest GetSumOfSingleServices [3] and its parallel line is GetServiceNames [3], then I would like GetServiceNames [3] to be my first entry in the list.

Not sure how to sort a list of strings by double values.

it

+4
source share
3 answers

For a general problem, I would use the Schwartz transform}, the general scheme used in Perl. It should be easy to pass the base method to .net

Your case is even simpler because you have full control over data access:

 var tuples = from dc in GetDashboardData group dc by dc.serviceType into g select new{ Cost = g.Sum(sc=>sc.serviceCost), Type = g.Key, }; var sorted = tuples.OrderByDescending(t=>t.Cost).Select(t=>t.Type); return sorted.ToList(); 
+3
source

With a source that looks like this, all your conversions become easy:

 var compositeList = GetDashboardData .GroupBy(dc => dc.serviceType) .Select(g => new{ name = g.Key, sum = g.Sum(sc => sc.serviceCost)}) .ToList(); 

(you might consider creating a specific class with two properties name and sum instead of an anonymous declaration of the object above).

Now:

 compositeList.OrderByDescending(x => x.sum).Select(x => x.name); 

etc.

Synchronized lists suck.

+6
source

You can implement the IComparable interface for the class you are requesting, or create a new class that implements the IComparer interface and passes it as a parameter to the sorting method.

+1
source

Source: https://habr.com/ru/post/1490955/


All Articles