Home > Enterprise >  How to display items in a list with a description of what that list item is in c# console
How to display items in a list with a description of what that list item is in c# console

Time:05-27

I am trying to display a list sorted in descending order with each item being displayed with a description of what the value of the item is in VS console app. I’m just not sure how to display the description with each item

Eg. output: Total monthly expenses//description of list item : $1000//item obtained from list and sorted Home loan repayment: $700 Vehicle Installment: $300

Thank you

CodePudding user response:

I cannot see where you are getting the description values from. A better way to do this would be to use Dictionary instead of a List.

double cost = textbox1.Text; // cost of item
string des = textbox2.Text; //description of item

//below code goes into a event to add each item cost description
Dictionary<double, string> io = new Dictionary<double, string>();
io.Add(cost, des);

//below code goes into a event to display all items
foreach(KeyValuePair<double, string> val in io.OrderByDescending(i => i.Key)) {
Console.WriteLine("${0},{1}", val.Key, val.Value);
}

CodePudding user response:

assuming you have fetched the data from the database, store the data in a dictionary collection. The key in the key-value pair of the dictionary set is the product description information, and the value is the specific price. You can first extract the value of the key in the dictionary set for sorting, and then extract the value value in the dictionary set according to the value of the key.

static void main(string[] args)
{
    Dictionary<string, int> d = new Dictionary<string, int>();

    d.Add("keyboard", 100);
    d.Add("mouse", 80);

    //get key
    var val = d.Keys.ToList();

    //sort
    val.Sort();

   
    foreach (var key in val)
    {
        Console.WriteLine(key);
    }
}
  • Related