>>> import re
>>> sorted(lst, key=lambda x: int(re.findall(r'\d+$', x)[0]))
['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']
Если вы используете char c1=c[3].nextItem();
, метод начинается с char
, а не из массива, и у вас нет знаний о массиве.
Предложение о создании настраиваемого метода для массива char
:
public static class MyExtensions
{
public static char nextItem(this char[] chars, int index)
{
//index start from 0
// my array length was bigger than 4 it returns c[4]
if(chars.length() > index+1)
return chars[index+1];
else
//it returns c[0]
return chars.Length() > 0 ? chars[0] : '';
}
}
И вызов:
char[] c = Console.ReadLine().ToCharArray();
var singleChar = c.nextItem(3);
Что-то вроде:
char c1 = (index < c.Length - 1) ? c[index].nextItem() : c[0];
Если вы хотите использовать метод, вы можете просто вернуть индекс:
char c1 = c[CheckIndex(index, c.Length())];
public static int CheckIndex(int index, int length)
{
return index < length - 1 ? index : 0;
}