I have an ArrayList that contains,
我有一个包含的ArrayList,
[0] = "1"
[1] = "10"
[2] = "2"
[3] = "15"
[4] = "17"
[5] = "5"
[6] = "6"
[7] = "27"
[8] = "8"
[9] = "9"
Now i need to sort the array list such that it becomes,
现在我需要对数组列表进行排序,使其成为,
[0] = "1"
[1] = "2"
[2] = "5"
[3] = "6"
[4] = "8"
[5] = "9"
[6] = "10"
[7] = "15"
[8] = "17"
[9] = "27"
At last i will be getting the values from ArrayList and using them as 'int' values. How can i do this? Or shall i convert them to int at first and then sort them.?
最后,我将从ArrayList获取值并将它们用作'int'值。我怎样才能做到这一点?或者我应该首先将它们转换为int然后对它们进行排序。
9 个解决方案
#1
If you can be sure the list contains only strings that can be transformed to integers, then with the IEnumerable<T>.OrderBy
extension method, try this:
如果你可以确定列表只包含可以转换为整数的字符串,那么使用IEnumerable
var sortedList = list.OrderBy(item => int.Parse(item));
If you're using an ArrayList
instead of a List<string>
(boo!), you'll need to Cast
first:
如果您使用的是ArrayList而不是List
var sortedList = list.Cast<string>().OrderBy(item => int.Parse(item));
You can also define your own comparer as JaredPar noted, but IMO that's a lot of work for something that's already implemented. However, it's more efficient.
你也可以像JaredPar所说的那样定义你自己的比较器,但IMO对于已经实现的东西来说有很多工作要做。但是,它更有效率。
#2
There are numerous sort methods in the framework including ArrayList.Sort. The problem is that they are all going to sort alphabetically and not numerically. You'll need to write a custom sorter that understands numeric sorts.
框架中有许多排序方法,包括ArrayList.Sort。问题是它们都是按字母顺序排序而不是数字排序。您需要编写一个了解数字排序的自定义排序器。
Try the following (some argument checking left out for brevity)
尝试以下(为简洁起见,遗漏了一些参数检查)
public class NumericComparer : IComparer {
public int Compare(object x, object y) {
string left = (string)x;
string right = (string)y;
int max = Math.Min(left.Length, right.Length);
for ( int i = 0; i < max; i++ ) {
if ( left[i] != right[i] ) {
return left[i] - right[i];
}
}
return left.Length - right.Length;
}
}
list.Sort(new NumericComparer());
#3
Implement custom comparer and pass it to ArrayList.Sort()
实现自定义比较器并将其传递给ArrayList.Sort()
Complete Code:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList a = new ArrayList();
a.Add("1");
a.Add("13");
a.Add("3");
a.Add("25");
a.Add("2");
a.Add("12");
a.Sort(new CustomComparer());
foreach (String s in a)
Console.WriteLine(s);
Console.Read();
}
}
public class CustomComparer : IComparer
{
Comparer _comparer = new Comparer(System.Globalization.CultureInfo.CurrentCulture);
public int Compare(object x, object y)
{
// Convert string comparisons to int
return _comparer.Compare(Convert.ToInt32(x), Convert.ToInt32(y));
}
}
}
Output:
1 2 3 12 13 25
1 2 3 12 13 25
#4
Maybe you could store the values in a strongly typed list like List instead, and the, if necessary, convert them to string, when cosuming them. Like this:
也许您可以将值存储在像List这样的强类型列表中,并在必要时将它们转换为字符串,当它们使用它们时。像这样:
List<int> intList = new List<int>(new int[] {3, 2, 1});
intList.Sort();
foreach (int theInt in intList)
{
System.Diagnostics.Debug.WriteLine(theInt.ToString());
}
#5
You'll be better of creating another array with Int
values and then sorting it with ArrayList.Sort()
. You could call ArrayList.Sort()
and pass it a delegate that will compare those strings as numbers but it will be slower. How much slower depends on size of your array and I personally think for sizes less then 100 it doesn't really matter.
您最好使用Int值创建另一个数组,然后使用ArrayList.Sort()对其进行排序。你可以调用ArrayList.Sort()并传递一个委托,将这些字符串作为数字进行比较,但速度会慢一些。慢多少取决于你的阵列的大小,我个人认为大小小于100它并不重要。
#6
If the values are all ints then why not store them as ints? That would make sorting easier and faster.
如果值都是整数,那么为什么不将它们存储为整数?这将使排序更容易,更快。
In what other ways are the values used? If they're only used as strings and only sorted once then it's probably sensible to leave them as they are - as strings.
使用的其他方式是什么?如果它们仅用作字符串并且仅排序一次那么将它们保持原样可能是明智的 - 作为字符串。
On the other hand, if they're used in maths ops then it's best to store them as ints.
另一方面,如果他们在数学运算中使用,那么最好将它们存储为整数。
#7
List<int> liDllCnt = new List<int>();
for (int temp = 0; temp < alFileName.Count; temp++)
liDllCnt.Add(Int32.Parse(alFileName[temp].ToString()));
liDllCnt.Sort();
alFileName is the name of the arraylist that i used.
alFileName是我使用的arraylist的名称。
#8
This is the safest way
这是最安全的方式
aryList is your ArrayList instance
aryList是您的ArrayList实例
object[] list = aryList.ToArray();
Array.Sort<object>
(
list,
delegate(object x, object y)
{
int a = 0, b = 0;
if (x == y) return 0;
if (x == null || y == null)
return x == null ? -1 : 1;
int.TryParse(x.ToString(), out a);
int.TryParse(y.ToString(), out b);
return a.CompareTo(b);
}
);
result saved into "list" object array
结果保存到“list”对象数组中
#9
If you can get the ArrayList items into a strongly typed container such as List<String> or String[] then Linq makes it easy to do the rest. The following implementation parses the string values only once and creates an anonymous type for each with the original string and its integer value.
如果您可以将ArrayList项目放入强类型容器(如List
public void Test_SortArrayList()
{
ArrayList items = new ArrayList(new []{"1", "10", "2", "15", "17", "5", "6", "27", "8", "9"});
string[] strings = (string[])items.ToArray(typeof(string));
List<string> result = strings
.Select(x => new
{
Original = x,
Value = Int32.Parse(x)
})
.OrderBy(x => x.Value)
.Select(x => x.Original)
.ToList();
result.ForEach(Console.WriteLine);
}
#1
If you can be sure the list contains only strings that can be transformed to integers, then with the IEnumerable<T>.OrderBy
extension method, try this:
如果你可以确定列表只包含可以转换为整数的字符串,那么使用IEnumerable
var sortedList = list.OrderBy(item => int.Parse(item));
If you're using an ArrayList
instead of a List<string>
(boo!), you'll need to Cast
first:
如果您使用的是ArrayList而不是List
var sortedList = list.Cast<string>().OrderBy(item => int.Parse(item));
You can also define your own comparer as JaredPar noted, but IMO that's a lot of work for something that's already implemented. However, it's more efficient.
你也可以像JaredPar所说的那样定义你自己的比较器,但IMO对于已经实现的东西来说有很多工作要做。但是,它更有效率。
#2
There are numerous sort methods in the framework including ArrayList.Sort. The problem is that they are all going to sort alphabetically and not numerically. You'll need to write a custom sorter that understands numeric sorts.
框架中有许多排序方法,包括ArrayList.Sort。问题是它们都是按字母顺序排序而不是数字排序。您需要编写一个了解数字排序的自定义排序器。
Try the following (some argument checking left out for brevity)
尝试以下(为简洁起见,遗漏了一些参数检查)
public class NumericComparer : IComparer {
public int Compare(object x, object y) {
string left = (string)x;
string right = (string)y;
int max = Math.Min(left.Length, right.Length);
for ( int i = 0; i < max; i++ ) {
if ( left[i] != right[i] ) {
return left[i] - right[i];
}
}
return left.Length - right.Length;
}
}
list.Sort(new NumericComparer());
#3
Implement custom comparer and pass it to ArrayList.Sort()
实现自定义比较器并将其传递给ArrayList.Sort()
Complete Code:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList a = new ArrayList();
a.Add("1");
a.Add("13");
a.Add("3");
a.Add("25");
a.Add("2");
a.Add("12");
a.Sort(new CustomComparer());
foreach (String s in a)
Console.WriteLine(s);
Console.Read();
}
}
public class CustomComparer : IComparer
{
Comparer _comparer = new Comparer(System.Globalization.CultureInfo.CurrentCulture);
public int Compare(object x, object y)
{
// Convert string comparisons to int
return _comparer.Compare(Convert.ToInt32(x), Convert.ToInt32(y));
}
}
}
Output:
1 2 3 12 13 25
1 2 3 12 13 25
#4
Maybe you could store the values in a strongly typed list like List instead, and the, if necessary, convert them to string, when cosuming them. Like this:
也许您可以将值存储在像List这样的强类型列表中,并在必要时将它们转换为字符串,当它们使用它们时。像这样:
List<int> intList = new List<int>(new int[] {3, 2, 1});
intList.Sort();
foreach (int theInt in intList)
{
System.Diagnostics.Debug.WriteLine(theInt.ToString());
}
#5
You'll be better of creating another array with Int
values and then sorting it with ArrayList.Sort()
. You could call ArrayList.Sort()
and pass it a delegate that will compare those strings as numbers but it will be slower. How much slower depends on size of your array and I personally think for sizes less then 100 it doesn't really matter.
您最好使用Int值创建另一个数组,然后使用ArrayList.Sort()对其进行排序。你可以调用ArrayList.Sort()并传递一个委托,将这些字符串作为数字进行比较,但速度会慢一些。慢多少取决于你的阵列的大小,我个人认为大小小于100它并不重要。
#6
If the values are all ints then why not store them as ints? That would make sorting easier and faster.
如果值都是整数,那么为什么不将它们存储为整数?这将使排序更容易,更快。
In what other ways are the values used? If they're only used as strings and only sorted once then it's probably sensible to leave them as they are - as strings.
使用的其他方式是什么?如果它们仅用作字符串并且仅排序一次那么将它们保持原样可能是明智的 - 作为字符串。
On the other hand, if they're used in maths ops then it's best to store them as ints.
另一方面,如果他们在数学运算中使用,那么最好将它们存储为整数。
#7
List<int> liDllCnt = new List<int>();
for (int temp = 0; temp < alFileName.Count; temp++)
liDllCnt.Add(Int32.Parse(alFileName[temp].ToString()));
liDllCnt.Sort();
alFileName is the name of the arraylist that i used.
alFileName是我使用的arraylist的名称。
#8
This is the safest way
这是最安全的方式
aryList is your ArrayList instance
aryList是您的ArrayList实例
object[] list = aryList.ToArray();
Array.Sort<object>
(
list,
delegate(object x, object y)
{
int a = 0, b = 0;
if (x == y) return 0;
if (x == null || y == null)
return x == null ? -1 : 1;
int.TryParse(x.ToString(), out a);
int.TryParse(y.ToString(), out b);
return a.CompareTo(b);
}
);
result saved into "list" object array
结果保存到“list”对象数组中
#9
If you can get the ArrayList items into a strongly typed container such as List<String> or String[] then Linq makes it easy to do the rest. The following implementation parses the string values only once and creates an anonymous type for each with the original string and its integer value.
如果您可以将ArrayList项目放入强类型容器(如List
public void Test_SortArrayList()
{
ArrayList items = new ArrayList(new []{"1", "10", "2", "15", "17", "5", "6", "27", "8", "9"});
string[] strings = (string[])items.ToArray(typeof(string));
List<string> result = strings
.Select(x => new
{
Original = x,
Value = Int32.Parse(x)
})
.OrderBy(x => x.Value)
.Select(x => x.Original)
.ToList();
result.ForEach(Console.WriteLine);
}