如何对ArrayList排序?单独的INT,请进

unixpeter 2009-03-22 12:47:22
ArrayList() a=new ArrayList();
a.Add(1);
a.Add(4);
a.Add(2);
如何正序和倒序排序?
结果要求: 1、2、4和4、2、1
...全文
758 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaxi 2010-01-12
  • 打赏
  • 举报
回复
mark
mindsky 2009-05-07
  • 打赏
  • 举报
回复
mark
呼唤 2009-04-15
  • 打赏
  • 举报
回复
看到好多兄弟都搞出个sort方法,真有sort方法吗?
gmliu_82 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wuyi8808 的回复:]
如果需要反序,则加上:Array.Reverse(t);


C# codeusing System;

class Program
{
static void Main()
{
string st = "1,4,2";
st = Sort(st);
Console.WriteLine(st); // 输出:4,2,1
}

static string Sort(string s)
{
string[] t = s.Split(',');
Array.Sort(t);
Array.Reverse(t); // 反序
return string.Join(",", t);
}
}
[/Quote]



简单扼要
bx_bob 2009-03-22
  • 打赏
  • 举报
回复
你问的是……冒泡排序法
还是说求一个现成的排序函数?
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
public class Order1 : System.Collections.IComparer
{
CaseInsensitiveComparer myComparer = new CaseInsensitiveComparer();
int System.Collections.IComparer.Compare(Object x, Object y)
{
return myComparer.Compare(y, x);
}
}
这个东西只是对整体的排序,得不到想要得结果,
这样返回的结果是
2,4,1
bx_bob 2009-03-22
  • 打赏
  • 举报
回复
学习到了……
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
string st = "1,4,2,"; 
st = st.Remove(st.Length - 1, 1); // 可以改为:st = st.Trim(',');
string[] k = st.Split('|'); // 应该是 st.Split(',')
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
如果需要反序,则加上:Array.Reverse(t);

using System;

class Program
{
static void Main()
{
string st = "1,4,2";
st = Sort(st);
Console.WriteLine(st); // 输出:4,2,1
}

static string Sort(string s)
{
string[] t = s.Split(',');
Array.Sort(t);
Array.Reverse(t); // 反序
return string.Join(",", t);
}
}
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System;

class Program
{
static void Main()
{
string st = "1,4,2";
st = Sort(st);
Console.WriteLine(st); // 输出:1,2,4
}

static string Sort(string s)
{
string[] t = s.Split(',');
Array.Sort(t);
return string.Join(",", t);
}
}
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
string st="1,4,2," ;
st = st.Remove(st.Length - 1, 1);
String[] k = st.Split('|');
ArrayList al = new ArrayList();
for (int i = 0; i < k.Length; i++)
{
if (!al.Contains(k[i]))
{
al.Add(k[i]);
}
}
al.Sort();
st = "";
foreach (string hh in al)
{
st += hh + ",";
}
st = st.Remove(st.Length - 1, 1);
return st;
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
st 还是 142或者241 根本没有用,
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
string st="1,4,2" ;
st = st.Remove(st.Length - 1, 1);
String[] k = st.Split('|');
ArrayList al = new ArrayList();
for (int i = 0; i < k.Length; i++)
{
if (!al.Contains(k[i]))
{
al.Add(k[i]);
}
}
al.Sort();
st = "";
foreach (string hh in al)
{
st += hh + ",";
}
st = st.Remove(st.Length - 1, 1);
return st;
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 unixpeter 的回复:]
如果1、4、2是字符串,也可以这样吗?
[/Quote]

可以。
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
对于反序排序,也可以这样:

using System;
using System.Collections;

public class ReverserCompare : IComparer
{
int IComparer.Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare(y, x);
}
}

class Program
{
static void Main()
{
ArrayList a = new ArrayList();
a.Add(1);
a.Add(4);
a.Add(2);
a.Sort(new ReverserCompare()); // 这时a的内容是:4 2 1
foreach (int x in a)
{
Console.WriteLine(x);
}
}
}

/* 程序输出:
4
2
1
*/
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
如果1、4、2是字符串,也可以这样吗?
wulongzan 2009-03-22
  • 打赏
  • 举报
回复
class Program
{
static void Main()
{
ArrayList a = new ArrayList();
a.Add(1);
a.Add(4);
a.Add(2);
a.Sort(); // 这时a的内容是:1 2 4
a.Reverse(); // 这时a的内容是:4 2 1
foreach (int x in a)
{
Console.WriteLine(x);
}
}
}

/* 程序输出:
4
2
1
*/
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System;
using System.Collections;

class Program
{
static void Main()
{
ArrayList a = new ArrayList();
a.Add(1);
a.Add(4);
a.Add(2);
a.Sort(); // 这时a的内容是:1 2 4
a.Reverse(); // 这时a的内容是:4 2 1
foreach (int x in a)
{
Console.WriteLine(x);
}
}
}

/* 程序输出:
4
2
1
*/
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System;
using System.Collections;

class Program
{
static void Main()
{
ArrayList a = new ArrayList();
a.Add(1);
a.Add(4);
a.Add(2);
a.Sort();
foreach (int x in a)
{
Console.WriteLine(x);
}
}
}

/* 程序输出:
1
2
4
*/
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
ArrayList() a = new ArrayList(); 
a.Add(1);
a.Add(4);
a.Add(2);
a.Sort();
加载更多回复(1)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧