首页 新闻 赞助 找找看

一个小小排序问题。。不是很高深的

0
悬赏园豆:5 [已解决问题] 解决于 2011-10-30 13:43

用c#代码实现,不要用linq技术

问题:

广东  30

湖南  20

广西  60

北京  70

上海  30

 

排序之后:

 

北京  70

广西  60

广东  30

上海  30

湖南  20

walleyekneel的主页 walleyekneel | 菜鸟二级 | 园豆:306
提问于:2011-10-27 13:52
< >
分享
最佳答案
0

方法一,二楼提出的冒泡排序:

class Program
{
static void Main(string[] args)
{
List<TestModel> tmlist = new List<TestModel>()
{
new TestModel(){city="广东",count=30},
new TestModel(){city="湖南",count=20},
new TestModel(){city="广西",count=60},
new TestModel(){city="北京",count=70},
new TestModel(){city="上海",count=30}
};

TestModel temp = new TestModel();
for (int m = 0; m < tmlist.Count; m++)
{
for (int n = 0; n < tmlist.Count - m - 1; n++)
{
if (tmlist[n].count < tmlist[n + 1].count)
{
temp = tmlist[n];
tmlist[n] = tmlist[n + 1];
tmlist[n + 1] = temp;
}
}
}

foreach (var item in tmlist)
{
Console.WriteLine(item.city + "" + item.count);
}

Console.Read();
}
}

public class TestModel
{
public string city { get; set; }
public int count { get; set; }
}

方法二,在重复项不是太多的情况下用:

class Program
{
static void Main(string[] args)
{
//原有数据
Dictionary<string, int> val = new Dictionary<string, int>();
val.Add("广东",30);
val.Add("湖南",20);
val.Add("广西",60);
val.Add("北京",70);
val.Add("上海",30);

SortedDictionary<int, string> newval = new SortedDictionary<int, string>(new ReverseComparer<int>());
int index = 1;
foreach (var item in val)
{
newval.Add(item.Value * 1000+index, item.Key);
index++;
}

val = new Dictionary<string, int>();
foreach (var item in newval)
{
val.Add(item.Value, item.Key / 1000);
Console.WriteLine(item.Value + "" + item.Key/1000);
}

Console.Read();
}
}

sealed class ReverseComparer<T> : IComparer<T> where T : IComparable<T>
{
public int Compare(T x, T y)
{
return y.CompareTo(x);
}
}

这个方法按你的水平想必能看明白,关键点在于 item.Value * 1000+index 这里的处理,最后得到的val就是排序后的字典。

收获园豆:5
artwl | 专家六级 |园豆:16736 | 2011-10-28 09:53
其他回答(4)
0

学下数据结构

失落小羊 | 园豆:58 (初学一级) | 2011-10-27 14:31

鬼都知道了。。字典。。?hashtable?list? 你写出来在跟我说。。不懂不要装b

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-10-27 14:33

@walleyekneel: 同学,你翻下数据结构的课本就知道了,代码可以照抄,我可以写给你,但你态度太差!

支持(0) 反对(0) 失落小羊 | 园豆:58 (初学一级) | 2011-10-27 14:35

还要告诉你:想直接求代码直接去百度好了,博客园是交流技术的,你连初级学生学的数据结构都不懂还上什么博客园,不如早点找个适合自己的正途吧

支持(0) 反对(0) 失落小羊 | 园豆:58 (初学一级) | 2011-10-27 14:44

@失落小羊: 你他妈不懂还装B,艹你全家啊。。逼哥来爆粗。。。中专生的孩子你瞧不起。。我看你水平跟我差不多

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-10-27 16:57

@walleyekneel: 孙子,你屁眼长脸上啦,没事回你的厕所吃屎去吧,你爸妈没教会你怎么做人啊,你爹生你个废柴出来很后悔吧,老子没看不起中专生,不过你他妈这态度跟小流氓一吊样,老子就看不起你,谁他妈愿意教你,我水平跟你差不多,不过一月拿几千玩似的,有空就来帮别人,不过像你这傻逼样的我就要骂你,不服你自己写个出来啊,别到这求人啊

支持(0) 反对(0) 失落小羊 | 园豆:58 (初学一级) | 2011-10-27 17:04

@失落小羊: 

有些人,不知道是你素质低还是你家长素质低。。。哥文化低。。粗话说出来在所难免。。哎。。跟比哥文化更低的人说话。。显得哥没文化。。看好 哥的技术

linq

SortedDictionary<string, int> dics =SortedDictionary<string, int>();

dics.Add("广东",30)

……

 var result = dics.OrderByDescending(p => p.Value);

foreach(var r in result)

{

   r.Key  

  r.Value

}

只是vs2005 不支持。。艹

你他妈才几千块?  滚去火星去吧,地球很危险啊

 

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-10-27 23:07

@walleyekneel: 孙子,都跟你说了看看数据结构里的排序方法了,还这么蠢,你用现成的api,不懂内部排序逻辑永远没长劲,我现在用的就是vs2005的;爷懒得和你废话了,你自己想想让你用c,c++怎么实现,想一辈子当民工那就这样傻逼吧

支持(0) 反对(0) 失落小羊 | 园豆:58 (初学一级) | 2011-10-28 10:20

@失落小羊: 我擦。。写不出来废话那么多。。借口一大堆。。。靠

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-10-30 13:42

@walleyekneel: 好吧,用c的话建个struct int,char[],随便用一种排序方法排序数字,得出结果。

你连个自己多傻逼都不知道,我把你光辉事迹转到论坛里了

支持(0) 反对(0) 失落小羊 | 园豆:58 (初学一级) | 2011-10-31 09:29

@失落小羊: 我艹你妹。。又逼哥爆粗。。。。哥就是没有看到一句代码。。。废话一大堆。。

写个给你参考

string[] arry = new string[5];
            arry[0] = "广东|30";
            arry[1] = "湖南|20";
            arry[2] = "广西|60";
            arry[3] = "北京|70";
            arry[4] = "上海|30";
            for (int i = 0; i < arry.Length-1; i++)
            {
                for (int j = i; j < arry.Length-1; j++)
                {
                    int num1=Convert.ToInt32(arry[j].Split('|')[1]);
                    int num2 = Convert.ToInt32(arry[j+1].Split('|')[1]);
                    if (num1>num2)
                    {
                        string temp = arry[j];
                        arry[j] = arry[j+1];
                        arry[j + 1] = temp;
                    }
                }
            }

            foreach (string item in arry)
            {
                Response.Write(item + "<br />");
            }

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-11-02 16:07
0

用个冒泡排序法就可以排好序了。

LCM | 园豆:6876 (大侠五级) | 2011-10-27 16:50

我知道啊。可是我存在字典中将key 跟value 颠倒,然后key值冒泡

key value

30 广东 

20 湖南 

60 广西 

70 北京

30 上海

可是字典中的key是不能重复。。我纠结很久都做不了

支持(0) 反对(0) walleyekneel | 园豆:306 (菜鸟二级) | 2011-10-27 17:00

@walleyekneel: 你完全可以用只有key和value两个属性的实体放到数组中,这样就不怕重复了啊。

支持(0) 反对(0) LCM | 园豆:6876 (大侠五级) | 2011-10-27 17:24
0

也可以将key放在一个数组里面排序,读取有序的时候按照数组顺序读取字典即可。

小小刀 | 园豆:1991 (小虾三级) | 2011-10-27 21:05
0

希望对你有帮助,这个方法不适合数据量太大的问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 控制台练习
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5];//城市对应的整数值
string[] str = new string[5];//城市数组
int temp = 0;
string strTemp = null;
/*-------初始化------*/
arr[0] = 30;
str[0] = "广东";
arr[1] = 20;
str[1] = "湖南";
arr[2] = 60;
str[2] = "广西";
arr[3] = 70;
str[3] = "北京";
arr[4] = 30;
str[4] = "上海";

//排序
for (int m = 0;m < arr.Length; m++)
{
for (int n = 0; n < arr.Length - m-1; n++)
{
if (arr[n] >arr[n+1])
{
temp = arr[n];
arr[n] = arr[n+1];
arr[n+1]=temp;
strTemp = str[n];
str[n] = str[n + 1];
str[n + 1] = strTemp;
}
}
}
for (int m = arr.Length - 1; m >= 0; m--)
{
Console.WriteLine("{0} {1}", str[m],arr[m]);
}
Console.ReadKey();
}
}
}

喬喬AI | 园豆:996 (小虾三级) | 2011-10-27 21:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册