1

That's my code...

string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis nisl vitae dolor tempus iaculis at id augue. Nullam metus mauris, viverra vitae tristique sed, pulvinar ac nulla."

List<object> listCarac = new List<object>();
object aux = 0;

text.Replace(" ", "");

for (int i = 0; i < text.Count(); i++)
{
    listCarac.Add(text.Substring(i,1));
}

for (int x = 0; x < listCarac.Count(); x++)
{
    for (int y = x + 1; y < listCarac.Count(); y++)
    {
        if (listCarac[x] > listCarac[y]) // My problem is here
        {
            aux = listCarac[x];
            listCarac[x] = listCarac[y];
            listCarac[y] = aux;
        }
    }
}

My problem is how to compare lexicographically, i guess the if is comparing alphabetically. Thanks.


Your list contains instances of type object which does not implement IComparable and thus you can´t call instance1 < instance2. However as you put only single characters into your list I guess you can use a List<char> instead or even simpler string instead of a list.

Thus you can now call this:

listCarac = listCarac.OrderBy(x => x).ToList();

Which will order your characters lexigraphically.

Furthermore when calling string.Replace the result is returned by the method as strings are immutable. So Within your first loop you use the original content of text instead of the replaced. Use this instead: text = text.Replace(" ", "");

EDIT: If - as you claim in your comments - you have to use a list of object and all the instances within this list implement IComparable (which char does) you can simply cast the instances to the interface before sorting:

var result = listCarac.OfType<IComparable>().OrderBy(x => x);
4

3 回答 3

1

object您的列表包含未实现的类型实例,IComparable因此您不能调用instance1 < instance2. 但是,当您仅将单个字符放入列表中时,我想您可以使用 aList<char>而不是甚至更简单的string 而不是列表。

因此,您现在可以这样称呼:

listCarac = listCarac.OrderBy(x => x).ToList();

这将按字典顺序排列您的字符。

此外,当调用string.Replace结果时,方法返回结果,因为字符串是不可变的。因此,在您的第一个循环中,您使用原始内容text而不是替换内容。改用这个:text = text.Replace(" ", "");

编辑:如果 - 正如您在评论中声明的那样 - 您必须使用该列表中的object所有实例的列表实现IComparablechar确实如此),您可以在排序之前简单地将实例转换为接口:

var result = listCarac.OfType<IComparable>().OrderBy(x => x);
于 2016-04-28T12:25:52.987 回答
0

We don't know much about object but we do know about IComparable items.

I would suggest you changing the list to

var listCarac = new List<IComparable>();

And than using the interface method CompareTo

int CompareTo(
    object obj
)

Which will change

if (listCarac[x] > listCarac[y])

To

if (listCarac[x].CompareTo(listCarac[y]) > 0)
于 2016-04-28T12:28:51.587 回答
0

I think you can take advantage of the SORT method provided for List. If necessary, you can provide a comparator to handle particular cases.

String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis nisl vitae dolor tempus iaculis at id augue. Nullam metus mauris, viverra vitae tristique sed, pulvinar ac nulla.";
text = text.Replace(" ", "");
List<char> demo = text.ToCharArray().ToList();
demo.Sort();
string result = System.Text.Encoding.UTF8.GetString(demo.Select(c => (byte)c).ToArray());
于 2016-04-28T12:48:17.173 回答