当前位置:神舟问>生活百科>arrays.sort

arrays.sort

2024-03-27 22:08:32 编辑:join 浏览量:624

问题补充说明:Book[] book = new Book[]{ new Book("name1",price1), new Book("name2",price2), new Book("name3",price3), } 其中Book是一个类有两个属性:书名和价格。 之后用Arrays.sort().的方法根据书的价格进行从小大的顺序排序。怎么派啊?

arrays.sort

使用TreeSet,给你贴个例子,把Dog换成你的Book就行了

publicclassTr推施西eeSetDemo{

publicstaticv材老李者林低oidmain(String[]args){

TreeS角是与氢章etts=newTreeSet(newDogComparate("age"));

ts.add(newDog("b动bb",8f,9f济希效括波质九费));

ts.add(360问答newDog("aaa",5f,6f));

Sy利经巴究固婷般茶stem.out.println(((D轮善触合川烧话万刻求og)ts.first()).getName());

}

}

//DogComparate比较物件

classDogComparateextendsComparate{

publicDogComparate(Stringattri脸问普事孔孙毛诗维速bute){

this.setAttribute(attribute);

}

p积量战间技础地ublicintname(Dogdog1,Dogdog2){

if(dog1.getName().equals(dog2革.getName())){

return0;

}else{

char[]char1=null;

char[]char2=null;

booleanb=true;

if(dog1.getName().length()<=dog2.getName().len且张余为草gth()){

char1=dog1.g击抗仍补映别度木etName().toCharArray();

char2=dog2.getName().toCharArray();

b=true;

}else{

char1=dog2.getName().toCharArray();

char2=dog1.getName().toCharArray();

b=false;

}

for(inti=0;i<char1.length;i++){

if(char1[i]==char2[i]){

continue;

}elseif(char1[i]>char2[i]){

retu重个古已质导酒rn(b?1:-1);

}elseif(c铁首演倒模har1[i]<char2[i]){

asserttrue;

return(b?-1:1);

}

}

return(b?-1:1);

}

}

publicintage(Dogdog1,Dogdog2){

if(dog1.getAge()>dog2.getAge()){

return1;

}elseif(dog1.getAge()==dog构歌反龙抗势2.getAge())看权施的参抓{

return0;

}el角板seif(dog1.getAge()<dog2.getAge()){

return-1;

}

return0;

}

}

//Comparate是作Comparator的抽象介面

classComparateimplementsComparator{

privateStringattribute;

publicStringgetAttribute(){

returnattribute;

}

publicvoidsetAttribute(Stringattribute){

this.attribute=attribute;

}

publicintcompare(Objectarg0,Objectarg1){

try{

Classcls=Class.forName(this.getClass().getName());

Class[]parTypes=newClass[]{arg0.getClass(),arg1.getClass()};

Methodmetd=cls.getMethod(this.attribute,parTypes);

ComparatemetdObj=this;

Object[]args=newObject[]{arg0,arg1};

Objectrtn=metd.invoke(metdObj,args);

return((Integer)rtn).intValue();

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SecurityExceptione){

e.printStackTrace();

}catch(NoSuchMethodExceptione){

e.printStackTrace();

}catch(IllegalArgumentExceptione){

e.printStackTrace();

}catch(IllegalAccessExceptione){

e.printStackTrace();

}catch(InvocationTargetExceptione){

e.printStackTrace();

}

return0;

}

}

//Dog物件,事实上你要用Cat物件都可以!!

classDog{

privatefloatage;

privateStringname;

privatefloatweight;

publicDog(Stringname,floatage,floatweight){

***.name=name;

this.age=age;

this.weight=weight;

}

publicvoidsetName(Stringname){

***.name=name;

}

publicStringgetName(){

return***.name;

}

publicvoidsetAge(floatage){

this.age=age;

}

publicfloatgetAge(){

returnthis.age;

}

publicvoidsetWeight(floatweight){

this.weight=weight;

}

publicfloatgetWeight(){

returnthis.weight;

}

}

标签:arrays,sort

版权声明:文章由 神舟问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.shenzhouwen.com/life/311098.html
热门文章