简体   繁体   中英

How to sort a string array with integers and strings in?

I have a string array: string[] face = {"A","2","3","4","5","6","7","8","9","10","J","Q","K" };

I randomized it and now I am trying to sort it back to what it was. I tried the Array.Sort() method but it didn't work.

Any ideas how I can achieve this?

You could use LINQs OrderBy method

var ordered = random.OrderBy(face => CardValue(face)).ToArray();

with the following CardValue method

int CardValue(string card)
{

    switch(card)
    {
        case "A":
            return 1;
        case "J":
            return 11;
        case "Q":
            return 12;
        case "K":
            return 13;
        default:
            return int.Parse(card);
    }
}

just to give you a gist of how this could be achieved. This sample is not really production-ready, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM