String List to Int List!?

Hello!
I have a List … full of numbers ( I can’t for my purposes simply creating a int list ).

Now I need to convert this String list (1,2,3,4,5) into a INT list.

Is there a simple way to do it, knowing that those strings are numbers only? (No need to check)

Thanks for your help … as usual!

You can use Linq;

IntList = StringList.Select(int.Parse).ToList();

or you could parse the strings in a loop;

        IntList = new List<int>();

        for(var i = 0; i < StringList.Count; i++)
        {
            IntList.Add(int.Parse(StringList[i]));
        }

Hi WarmedxMints!
Thanks for your reply … I used Linq method of course but now I have a new problem

I Can’t insert those converted values into Dropbox options! Something like "Can’t convert from System.Collection.Generic.List to System.Collection.Generic.List<UnityEngine.UI.Dropdown.OptionData>.

Old String List can be used!

:frowning:

The dropdown AddOptions method has 3 overloads, List<Dropdown.OptionData>, List and a List.

Just add the string list. What is it you are trying to do and why do you want to add ints rather that strings?

I See …
I’d need to convert that string list to an int list because I am trying to sort in the correct way the dropdown.
I am retrieving data from an online SQL database and sorting that numbers by using

filtroN_CYL.Sort()

isn’t working good as I get: 10,12,4,6 instead of 12,10,6,4. I thought that converting strings into integers i could sort numbers in a better way! Unfortunately those data should sort automatically as database is changing!

Ehm … Thanks again … now I quit and I will try again tomorrow! (Late night here!) :wink:

Do you just want to sort descending in int value? You can use the list sort and a delegate for that.

StringList.Sort(delegate (string a, string b)
{
    if (int.Parse(a) < int.Parse(b))
        return 1;
    else if (int.Parse(a) > int.Parse(b))
        return -1;
    return 0;
});