首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

对二个arrylist排序有关问题

2012-02-16 
对二个arrylist排序问题第一个ArrayList存放着一些数字4,6,8,3第二个ArrayList是对应这些数字的String:b,d

对二个arrylist排序问题
第一个ArrayList   存放着一些数字4,6,8,3
第二个ArrayList   是对应这些数字的String:b,d,c,f
现在我想让第一个ArrayList   按升序排,然后第二个ArrayList   顺序也跟着第一个ArrayList   一样。
比如排序后变成:
3,4,6,8
f,b,d,c

[解决办法]
for(int i=0;i <ArrayList1.lenght;i++){
for(int j=i;j <ArrayList1.lenght;j++){
if(ArrayList1[i] <ArrayList1[j]){
int tmp1;
tmp1=ArrayList1[i];
ArrayList1[i]=ArrayList1[j];
ArrayList1[j]=tmp1;

char tmp2;
tmp2=ArrayList2[i];
ArrayList2[i]=ArrayList2[j];
ArrayList2[j]=tmp2;
}
}
}
[解决办法]
呵呵,上面给了答案了
关键的用char定义字符,就可以得到字符的ASCII码,
char是存放0-255的整型,就可以进行排序了。
[解决办法]
这几天在学习Hashtable的使用,不知道我用的方法是不是很笨,仅供楼主参考,同时静候高人答案

      int[] arr1 = {3,4,6,8};
string[] arr2 = { "f ", "b ", "d ", "c "};
ArrayList list1 = new ArrayList(arr1);
ArrayList list2 = new ArrayList(arr2);
Hashtable ht = new Hashtable();
InitHashTable(ht, list1, list2);
list1.Sort();
for (int i = 0; i < list2.Count; i++)
{
list2[i] = ht[list1[i]].ToString();
}


private static void InitHashTable(Hashtable ht, ArrayList l1, ArrayList l2)
{
for (int i = 0; i < l1.Count; i++)
{
ht.Add(l1[i], l2[i]);
}
}
[解决办法]
用sortedList排序, 按key自动排序
arrayA 数字, arrayB 字母
Dim list As New SortedList
list.Add(arrayA, arrayB)

[解决办法]
直接用list.Sort()不就得了,干嘛这么麻烦哦
[解决办法]
int[] arr1 = {4,6,8,3};
string[] arr2 = { "b ", "d ", "c ", "f "};
Array.Sort(arr1, arr2);
for(int i=0;i <arr1.Length;i++){
Console.WriteLine( "{0}:{1}\t{2} ",i,arr1[i],arr2[i]);
}

====输出
0:3 f
1:4 b
2:6 d
3:8 c
END

[解决办法]
用sort方法,简单,而且容易扩展支持多种排序方式。

see:
------------------
using System;
using System.Collections;

public class SamplesArrayList {

public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}

}

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The " );
myAL.Add( "quick " );
myAL.Add( "brown " );
myAL.Add( "fox " );
myAL.Add( "jumps " );
myAL.Add( "over " );
myAL.Add( "the " );
myAL.Add( "lazy " );
myAL.Add( "dog " );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values: " );


PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine( "After sorting with the default comparer: " );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( myComparer );
Console.WriteLine( "After sorting with the reverse case-insensitive comparer: " );
PrintIndexAndValues( myAL );

}

public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1} ", i++, obj );
Console.WriteLine();
}

}


/*
This code produces the following output.
The ArrayList initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog

After sorting with the default comparer:
[0]: brown
[1]: dog
[2]: fox
[3]: jumps
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The

After sorting with the reverse case-insensitive comparer:
[0]: the
[1]: The
[2]: quick
[3]: over
[4]: lazy
[5]: jumps
[6]: fox
[7]: dog
[8]: brown
*/

热点排行