java排序问题

meizi198796 2015-04-15 05:34:20
有这样一个需求:现在有一个数组或者map 一个分数对应一个名称:
3:“aa”,2:"dd",8:"ff",3:"cc"
现在需要先按照分数进行从大到小排序如果有相同分数的在按照字母来排序,最后拿出来最大的两组数据(8:“ff”,3:"aa"),不晓得java怎么来实现了,希望大神帮帮忙
...全文
355 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
meizi198796 2015-04-17
  • 打赏
  • 举报
回复
[quote=引用 1 楼 wangxf_8341 的回复:] 下面是基本思路


import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;


public class ScoreOrder {

    public static void main(String[] args) {
        //如果分数作为key,就不能用map了,因为不允许存在同样的key多个value
        List<Score> list = new ArrayList<Score>();
        list.add(new Score(2, "dd"));
        list.add(new Score(8, "ff"));
        list.add(new Score(3, "cc"));
        list.add(new Score(3, "aa"));
        Score[] scores = list.toArray(new Score[] {});
        Arrays.sort(scores);
        System.out.println(Arrays.asList(scores));
    }

     

    public static class Score implements Comparable<Score> {

        private Integer point;
        private String  level;
        public Score(Integer point,String  level){
            this.point = point;
            this.level = level;
        }
        @Override
        public int compareTo(Score o) {
            if (point > o.point) {
                return 1;
            } else if (point < o.point) {
                return -1;
            } else {
                return level.compareTo(o.level);
            }
        }

        public String toString() {
            return "point:" + point + " & level:" + level;
        }
    }
}

[/quot]采用,多谢了。
microhex 2015-04-16
  • 打赏
  • 举报
回复
引用 6 楼 tianfang 的回复:
发个map的



import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AA {
	
	 
    public static void main(String[] args) {
        //如果分数作为key,就不能用map了,因为不允许存在同样的key多个value
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(2, "dd");
        map.put(8, "ff");
        map.put(3, "cc");
        map.put(3, "aa");
       Integer[]  keys=map.keySet().toArray(new Integer[0]);
        Arrays.sort(keys);
        System.out.println(map.get(keys[0]));
    }
 }

MAP的key值不允许重复啊,这样做有些问题啊。。。
tianfang 2015-04-16
  • 打赏
  • 举报
回复
上面是抄了一楼的代码改的 一楼 别介意哦
tianfang 2015-04-16
  • 打赏
  • 举报
回复
发个map的



import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AA {
	
	 
    public static void main(String[] args) {
        //如果分数作为key,就不能用map了,因为不允许存在同样的key多个value
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(2, "dd");
        map.put(8, "ff");
        map.put(3, "cc");
        map.put(3, "aa");
       Integer[]  keys=map.keySet().toArray(new Integer[0]);
        Arrays.sort(keys);
        System.out.println(map.get(keys[0]));
    }
 }

suciver 2015-04-16
  • 打赏
  • 举报
回复
方法很多,把分数和名称作为一个Pojo的属性,可以用Arrays.sort(Object[]),Collections.sort(List<T>),TreeSet
scott_129 2015-04-16
  • 打赏
  • 举报
回复
方法都多中,可以用map,里面用数字作为key,用list作为value,如果数字相同的,就把对应的字符add到list中, 另外一种就是直接放到list中,自己构建一个类,定义数字和字符两个属性,然后重写compareto方法,就拿数字比较,相同的再比较字符,这样再调用集合的sort方法排序就可以了, 当然还有其他的方法。
娜可哈哈 2015-04-15
  • 打赏
  • 举报
回复
//按照分数从高到低和对应的字母的自然顺序 import java.util.Map; import java.util.Iterator; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; public class J_test3 { public static void main(String []args) { TreeMap<String,Integer> t0=new TreeMap<String,Integer>(); t0.put("aa",3); t0.put("dd",2); t0.put("ff",8); t0.put("cc",3); Show0("存储的Map集合"); Tmcount(t0); } public static void Show0(Object o) { System.out.println(o); } // 定义静态方法传入Map集合返回最大的两组映射关系 // 因为分数有重复,所以分数不能作为键,将字母作为键,分数作为键值 // 先比分数,在比较分数映射的名称 public static void Tmcount(TreeMap<String,Integer> t) { Show0(t); TreeSet<T0> Ts=new TreeSet<T0>(); // 以enterSet();方式取出 Set<Map.Entry<String, Integer>> s=t.entrySet(); for(Iterator<Map.Entry<String, Integer>> Ir=s.iterator();Ir.hasNext()==true;) { Map.Entry<String, Integer> Me=Ir.next(); String Name=Me.getKey(); int Num=Me.getValue(); Ts.add(new T0(Num,Name)); } for(int i=0;i<2;i++) { T0 t1=Ts.pollFirst(); Show0(t1.Setnum()+"- -"+t1.SetName()); } } } //自定义类 class T0 implements Comparable<T0> { private int Num; private String Name; T0(int Num,String Name) { this.Num=Num; this.Name=Name; } // 覆写hashCode();如果存储在HashSet集合中,第一步比较hashCode值,若不同将该对象存储在HashSet中 public int hashCode() { return this.Name.hashCode()+this.Num*14; } // 若上一步的hashCode值相同,进一步比较是否是同一个对象,是同一个对象不存储 public boolean equals(T0 t) { return this.Name.equals(t.Name)&&this.Num==t.Num; } // 覆写compareTo();方法,用于比较对象的自然顺序 public int compareTo(T0 t) { int num=new Integer(this.Num).compareTo(new Integer(t.Num)); if(num==0) return -this.Name.compareTo(t.Name); return -num; } public int Setnum() { return this.Num; } public String SetName() { return this.Name; } } 我也是新手,共同学习!
scmod 2015-04-15
  • 打赏
  • 举报
回复
map的话可以把key拿出来Collections.sort(new ArrayList(#map#.keySet())) 然后这个list就是排序完的key把头尾那个拿来get下就好了.. 数字的key就是这样~
淡定的峰哥 2015-04-15
  • 打赏
  • 举报
回复
下面是基本思路


import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;


public class ScoreOrder {

    public static void main(String[] args) {
        //如果分数作为key,就不能用map了,因为不允许存在同样的key多个value
        List<Score> list = new ArrayList<Score>();
        list.add(new Score(2, "dd"));
        list.add(new Score(8, "ff"));
        list.add(new Score(3, "cc"));
        list.add(new Score(3, "aa"));
        Score[] scores = list.toArray(new Score[] {});
        Arrays.sort(scores);
        System.out.println(Arrays.asList(scores));
    }

     

    public static class Score implements Comparable<Score> {

        private Integer point;
        private String  level;
        public Score(Integer point,String  level){
            this.point = point;
            this.level = level;
        }
        @Override
        public int compareTo(Score o) {
            if (point > o.point) {
                return 1;
            } else if (point < o.point) {
                return -1;
            } else {
                return level.compareTo(o.level);
            }
        }

        public String toString() {
            return "point:" + point + " & level:" + level;
        }
    }
}

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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