About Sort by List Elements (OrderBy) and Assign Any Number

Asked 2 years ago, Updated 2 years ago, 50 views

We are creating a fruit distribution program.

I don't know how to handle the list and sort the list by the elements in the list.Once that is done, I would like to assign any number to each item in the list by loop.

  • The list contains information about the person's name and age (Mr. + First Name + Age) and is added by user input. (Is it smarter to have a multidimensional array of names and ages?)
  • I want to sort the list by age
  • I would like to add any number of inputs to the information in the list in order.(Images that are distributed one by one until the fruit is gone in the loop instead of the same number by division)

Code

List<string>names=new List<string>();;    
private void BtnDivide_Click(object sender, RoutedEventArgse)
{
    intnrOfFruits=int.Parse(TxtTotalFruits.Text);
    intc=names.Count;
    int division;
    string firstName = TxtFirstName.Text;
    string lastName = TxtLastName.Text;      
    string txtAge=TxtAge.Text;           

    // If we divide it equally by division, we will have surplus depending on the situation, so we would like to distribute it to the last one in the loop instead of running a business eventually.

    division = nrOfFruits/c;

    for (inti=0;i<c;i++)
    {
        // I would like to sort them in ascending order using the age elements in the list.

        // Lambda operator:=> 

        // Use OrderBy?
        var orderedNames=names.OrderBy(txtAge=>names);

        // Connect the list box NameList and orderedNames on wpf
        NameList.Items[i] = orderedNames;

        // Once the list is sorted in the desired order, the number is displayed in the list box on the wpf.

        NameList.Items[i]+="+division.ToString()+"fruit";
    }
}

c#

2022-09-30 21:37

2 Answers

Let's take turns cleaning up each item.

Let's start with the first condition.Let's sort out what list items you have and how you want the list to be displayed to users.
This time, the list has a list of people, and each list item is

  • Age
  • Last name
  • Name
  • Number of fruits

You have indicated that you would like to have .
The way you hold your data is smart enough to manage them as classes and structures.

It's the second one.If you want to use OrderBy when sorting lists, the argument specifies a lambda expression to get the key to sort.The image is that all the elements in the list are converted into a given lambda formula and sorted by the result.However, this is the that sorts the original list, not the original list itself. If you want to sort the list itself, there is a method called Sort if it is in the List<T> class.

And about the third fruit distribution."This time, regardless of the type of fruit, they simply wanted to equalize the number as much as possible."And I heard that the remaining amount will be distributed from the younger people in order.
If so,

// The number of fruits to distribute equally to everyone.The C# specification truncates the decimal portion of the result when divided by integers.
var fruitsDiv =nrOfFruits/peopleList.Count;
// The number of fruits left over, that is, the number of people who can get one more fruit.
var fruitsMod =nrOfFruits%peopleList.Count;

// PeopleList is a list of people sorted by age.
// Suppose you have a property called FruitCount to remember the number of fruits.
for (vari=0;i<peopleList.Count;i++)
{
    // Only young people to fruitsMod distribute fruitsDiv and one more.
    if(i<fruitMod)
    {
        peopleList[i].FruitsCount=fruitsDiv+1;
    }
    else
    {
        peopleList[i].FruitsCount=fruitsDiv;
    }
}

It would be a good idea to do something like that

By the way, the part of orderedNames becomes System.Linq.orderedEnumerable... because orderedNames has been implicitly converted into a string.By default, the passed object has
The value returned by the ToString method is included there.If you extend ToString when you create your own class, you are free to rewrite this conversion result.A simple listing might do this without creating a template, but if you want to control more complex viewing, you might want to learn more about the WPF template or ask another question.


2022-09-30 21:37

Is it smarter to have a multi-dimensional array by name and age?

Data structures should be defined in classes or structures for easy access.

public class Person
    {
        public string FirstName {get;set;}
        public string LastName {get;set;}
        public int Age {get;set;}
        public int fruits {get;set;}
    }

The OrderBy argument specifies a function that extracts the key to be sorted from the element.In the example below, it is described in lambda formula.

private void SortAndDistributeFruits (List<Person>persons, int fruits)
    {
        // Sort by age
        var orderedPersons=persons.OrderBy(e=>e.age).ToList();

        // distribute one by one until the fruit is gone in the loop
        for (inti=0; i<fruit;i++)
        {
            // divide by the number of people to decide who to distribute
            var index = i%orderedPersons.Count;

            // hand out one
            orderedPersons [index].Fruits++;
        }
    }

The list returned by OederBy is different from the original list.If you want to change the order of the original list itself, you can also use the Sort method.


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.