使用蛮力按字典顺序排列 List<object>
Order a List<object> lexicographically using brute force
这是我的代码...
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;
}
}
}
我的问题是如何按字典顺序比较,我猜 if 是按字母顺序比较。
谢谢
您的列表包含未实现 IComparable
的 object
类型的实例,因此您无法调用 instance1 < instance2
。但是,当您只将单个字符放入列表中时,我想您可以使用 List<char>
代替或更简单的 string
代替列表。
因此您现在可以这样称呼:
listCarac = listCarac.OrderBy(x => x).ToList();
这将按字典顺序排列您的字符。
此外,当调用 string.Replace
时,方法会返回结果,因为字符串是不可变的。因此,在您的第一个循环中,您使用 text
的原始内容而不是替换的内容。改用这个:text = text.Replace(" ", "");
编辑:如果 - 正如您在评论中所声称的那样 - 您必须使用 object
列表并且此列表中的所有实例都实现 IComparable
( char
执行)您可以在排序之前简单地将实例转换为接口:
var result = listCarac.OfType<IComparable>().OrderBy(x => x);
我们对 object
了解不多,但我们知道 IComparable 项目。
我建议您将列表更改为
var listCarac = new List<IComparable>();
与使用接口方法相比CompareTo
int CompareTo(
object obj
)
哪个会改变
if (listCarac[x] > listCarac[y])
到
if (listCarac[x].CompareTo(listCarac[y]) > 0)
我认为您可以利用为 List 提供的 SORT 方法。如有必要,您可以提供一个比较器来处理特定情况。
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());
这是我的代码...
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;
}
}
}
我的问题是如何按字典顺序比较,我猜 if 是按字母顺序比较。 谢谢
您的列表包含未实现 IComparable
的 object
类型的实例,因此您无法调用 instance1 < instance2
。但是,当您只将单个字符放入列表中时,我想您可以使用 List<char>
代替或更简单的 string
代替列表。
因此您现在可以这样称呼:
listCarac = listCarac.OrderBy(x => x).ToList();
这将按字典顺序排列您的字符。
此外,当调用 string.Replace
时,方法会返回结果,因为字符串是不可变的。因此,在您的第一个循环中,您使用 text
的原始内容而不是替换的内容。改用这个:text = text.Replace(" ", "");
编辑:如果 - 正如您在评论中所声称的那样 - 您必须使用 object
列表并且此列表中的所有实例都实现 IComparable
( char
执行)您可以在排序之前简单地将实例转换为接口:
var result = listCarac.OfType<IComparable>().OrderBy(x => x);
我们对 object
了解不多,但我们知道 IComparable 项目。
我建议您将列表更改为
var listCarac = new List<IComparable>();
与使用接口方法相比CompareTo
int CompareTo(
object obj
)
哪个会改变
if (listCarac[x] > listCarac[y])
到
if (listCarac[x].CompareTo(listCarac[y]) > 0)
我认为您可以利用为 List 提供的 SORT 方法。如有必要,您可以提供一个比较器来处理特定情况。
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());