A. Vreeswijk
A. Vreeswijk

Reputation: 954

Order a List a specific way

I have a problem. I have a list with 4 coins and 4 values. Now the List is sorted by name like this:

 1. BTC---Value
 2. ETH---Value
 3. LTC---Value
 4. USDT---Value

But now I want to get a List with only 2 coins left: The last coin needs to be USDT and the first Coin is the coin with the highest value. So for example if all the coins have value 3 and BTC has value 4, then I want a List like this:

 1. BTC---Value
 2. USDT---Value

How can I do that, because I know how to sort by value, but not with all my preferences....

Can someone help me?

DETAILS
Even if USDT has the highest value, I want that coin at the last place. If you add another coin, it needs to just look at the highest value again (except for USDT) and place that coin at the top with USDT on second place!

Upvotes: 0

Views: 116

Answers (4)

stannius
stannius

Reputation: 1307

You could do it with Linq. This wouldn't modify the list; it would create a new enumerable sorted by your criteria.

var sortedCoins = coins.OrderBy(c => c.Name == "USDT")
                       .ThenByDescending(c => c.Value);

Upvotes: 1

Anu Viswan
Anu Viswan

Reputation: 18163

Updated code based on comment by DubDub.

 var intermediate =  list.OrderBy(x=> x.Name=="USDT").ThenByDescending(x=>x.Value);
 var result = new []{intermediate.First(),intermediate.Last()};

Example, Scenario 1 : When there are more than 2 items

 var list = new List<Coin>
 {
    new Coin{Name="USDT", Value = 29},
    new Coin{Name="ETH", Value = 13},
    new Coin{Name="LTC", Value = 21},
    new Coin{Name="BTC", Value = 3},
 };

Output

enter image description here

Scenario 2 : When there are only two items

 var list = new List<Coin>
 {
    new Coin{Name="USDT", Value = 29},
    new Coin{Name="LTC", Value = 21},
 };

enter image description here

Upvotes: 2

Marco Salerno
Marco Salerno

Reputation: 5201

This way:

class Program
{
    static void Main(string[] args)
    {
        List<Coin> coins = new List<Coin>()
        {
            new Coin ("BTC", 1),
            new Coin ("ETH", 2),
            new Coin ("LTC", 3),
            new Coin ("USDT", 4),
        };

        Coin usdt = coins.First(x => x.Name == "USDT");

        coins.Remove(usdt);

        coins = new List<Coin>() { coins.OrderByDescending(x => x.Value).First(), usdt };
    }
}

public class Coin
{
    public string Name { get; set; }
    public double Value { get; set; }

    public Coin(string name, double value)
    {
        Name = name;
        Value = value;
    }
}

Upvotes: 0

DubDub
DubDub

Reputation: 1387

Using the following stolen class from a previous answer that is now gone so I'm not sure who to give credit to, but you should be able to do the following.

Coin class

public class Coin
{
    public string Name { get; set; }
    public double Value { get; set; }
}

Actual code to handle list

List<Coin> list = new List<Coin>
{
    new Coin{Name="USDT", Value = 29},
    new Coin{Name="ETH", Value = 13},
    new Coin{Name="LTC", Value = 21},
    new Coin{Name="BTC", Value = 3},
};

// Take out USDT coin
Coin USDTcoin = list.Find(coin => coin.Name == "USDT");
list.RemoveAt(list.FindIndex(coin => coin.Name == "USDT"));

// Order list
list = list.OrderByDescending(coin => coin.Value).ToList();

// Make new list
list = new List<Coin>
{
    list[0],
    USDTcoin
};

Upvotes: 0

Related Questions