码迷,mamicode.com
首页 > 其他好文 > 详细

sort quick

时间:2014-09-15 00:58:47      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   ar   for   

bubuko.com,布布扣
package com.demo;

import java.util.ArrayList;
import java.util.List;

public class SearchSort
{
        public  List<TestBean> quickSort(List<TestBean> list, int from, int to) { 
             if (from < to) { 
                 TestBean temp = list.get(to); 
                 int i = from - 1; 
                 for (int j = from; j < to; j++) { 
                     if (compare(list.get(j), temp)) { 
                         i++; 
                         TestBean tempValue = list.get(j);
                         list.set(j, list.get(i));
                         list.set(i, tempValue);
                     } 
                 } 
                 list.set(to, list.get(i+1));
                 list.set(i+1,temp);
                 quickSort(list, from, i); 
                 quickSort(list, i + 1, to); 
             }
            return list;
         } 
        
        public boolean compare(TestBean testBean, TestBean temp){
            
            
            /*
            if (testBean.score < temp.score) 
                return true;
            else if(testBean.score == temp.score){
                if(testBean.id().compareTo(temp.id())<=0)
                    return true;
                else
                    
                    return false;
            }*/
            if(testBean.id.compareTo(temp.id) < 0)
            {
                return true;
            }
            else
                return false;
        }
       

        public static void main(String[] args)
        {
            List<TestBean> list = new ArrayList<TestBean>();

            TestBean bean = new TestBean();
            bean.author = "ninhao";
            bean.date = "10101";
            bean.id = "2342342341";
            bean.score = 3;

            
            TestBean bean3 = new TestBean();
            bean3.author = "ninhao3";
            bean3.date = "10101";
            bean3.id = "2342342340";
            bean3.score = 4;

            
            TestBean bean2 = new TestBean();
            bean2.author = "ninha2";
            bean2.date = "10101";
            bean2.id = "2342342343";
            bean2.score = 0;

            
            TestBean bean1 = new TestBean();
            bean1.author = "ninhao1";
            bean1.date = "10101";
            bean1.id = "234234";
            bean1.score = 2;
            
            
            list.add(bean);
            list.add(bean1);
            list.add(bean2);
            list.add(bean3);
            
            System.out.println(list.size());
            SearchSort SearchSort =new SearchSort();
            list = SearchSort.quickSort(list, 0, list.size()-1);
                    
            for(TestBean beane : list)
            {
                System.out.println(beane.score + " " + beane.id + " " +beane.author);
            }
        }

}
test
bubuko.com,布布扣
package com.demo;

public class TestBean
{
    public String id;
    public String title;
    public String keywords;
    public String describe;
    public String date;
    public String url;
    public String kind;
    public String author;
    public String publisher;
    public String content;
    public String height;
    public String width;
    public String mp4Url;
    public String imageUrl;
    public int score;
    public int num;
}
bean

 

sort quick

标签:des   style   blog   http   color   os   java   ar   for   

原文地址:http://www.cnblogs.com/feiguo/p/3971956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!