您的位置:首页 > 移动开发 > Android开发

排序算法Java(android)安卓中的list对象排序

2013-05-21 22:18 519 查看
今天一直在学习排序算法之类的问题,上午正好有机会和大家讨论一下.

由于开发百度舆图获取到的POI信息须要进行由进到远的排序。所以须要些排序算法。

之前是采取冒泡排序算法

算法如下:(摘自网络)

public class MaoPaoSort {
public static void main(String[] args) {
try {
int mp[] = new int[args.length];
for (int i = 0; i < args.length; i++) {
mp[i] = Integer.parseInt(args[i]);
}
for (int i = 0; i < mp.length; i++) {
for (int j = 0; j < mp.length; j++) {
int temp;
if (mp[i] < mp[j]) {
temp = mp[j];
mp[j] = mp[i];
mp[i] = temp;
}
}
}
for (int i = 0; i < mp.length; i++) {
System.out.print(mp[i] + " ");
}

进过搜索发现,本来JAVA工具类已经有排序算法了,遂用之。

Collections类下的sort方法

static <T> voidsort( List<T> list, Comparator<? super T> comparator)
Sorts the specified list using the specified comparator.
static <T extends Comparable<? super T>> voidsort( List<T> list)
Sorts the specified list in ascending natural order.
两种方法都能实现排序。

好了,下面是我们须要排序的对象。

一个用于存放通过百度POI搜索失掉的结果的对象

package com.android.so.model;

import com.baidu.platform.comapi.basestruct.GeoPoint;

public class PoiInfo {
private String name;
private String address;
private boolean detail;
private String uid;
private String telephone;
private GeoPoint pt;
private int distance;
private int ePoiType;

public String getName() {
return name;
}

public String getAddress() {
return address;
}

public String getUid() {
return uid;
}

public String getTelephone() {
return telephone;
}

public GeoPoint getPt() {
return pt;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(String address) {
this.address = address;
}

public void setUid(String uid) {
this.uid = uid;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setPt(GeoPoint pt) {
this.pt = pt;
}

public int getePoiType() {
return ePoiType;
}

public void setePoiType(int ePoiType) {
this.ePoiType = ePoiType;
}

public boolean isDetail() {
return detail;
}

public void setDetail(boolean detail) {
this.detail = detail;
}

public int getDistance() {
return distance;
}

public void setDistance(int distance) {
this.distance = distance;
}

}

当初我们须要他按照Distance(距离)来进行升序排序。

这里使用方法:

每日一道理

古人云:“海纳百川,有容乃大。”人世间,不可能没有矛盾和争吵,我们要以磊落的胸怀和宽容的微笑去面对它 。哈伯德也曾说过:“宽恕和受宽恕的难以言喻的快乐,是连神明都会为之羡慕的极大乐事。”让我们从宽容中享受快乐,从谅解中体会幸福吧!

static <T> void
sort(
List<T> list,
Comparator<? super T> comparator)

Sorts the specified list using the specified comparator.

我们就须要调用方法:

Collections.sort(poiArrayList, new Comparator<PoiInfo>() {

@Override
public int compare(PoiInfo lhs, PoiInfo rhs) {
return lhs.getDistance() - rhs.getDistance();
}
});

同理如果须要进行降序排序,只须要更改return的结果,如下:

Collections.sort(poiArrayList, new Comparator<PoiInfo>() {

@Override
public int compare(PoiInfo lhs, PoiInfo rhs) {
return rhs.getDistance() - lhs.getDistance();
}
});

----------------------------------------------------------------------------------------------------分割线--------------------------------------------------------------------------------------------------------------------

以下是另一种方法

static <T extends Comparable<? super T>> voidsort( List<T> list)
Sorts the specified list in ascending natural order.
这种方法须要对象本身实现Comparable<T>接口。

所以PoiInfo类改为:

package com.android.so.model;

import com.baidu.platform.comapi.basestruct.GeoPoint;

public class PoiInfo implements Comparable<PoiInfo> {
private String name;
private String address;
private boolean detail;
private String uid;
private String telephone;
private GeoPoint pt;
private int distance;
private int ePoiType;

public String getName() {
return name;
}

public String getAddress() {
return address;
}

public String getUid() {
return uid;
}

public String getTelephone() {
return telephone;
}

public GeoPoint getPt() {
return pt;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(String address) {
this.address = address;
}

public void setUid(String uid) {
this.uid = uid;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setPt(GeoPoint pt) {
this.pt = pt;
}

public int getePoiType() {
return ePoiType;
}

public void setePoiType(int ePoiType) {
this.ePoiType = ePoiType;
}

public boolean isDetail() {
return detail;
}

public void setDetail(boolean detail) {
this.detail = detail;
}

public int getDistance() {
return distance;
}

public void setDistance(int distance) {
this.distance = distance;
}

@Override
public int compareTo(PoiInfo another) {
return getDistance() - another.getDistance();
}

}

实现了compareTo方法,以上是升序排序,如果须要降序排序,将return getDistance() - another.getDistance();改为return another.getDistance() - getDistance();即可。

OK ,那么我们就须要调用代码:

Collections.sort(poiArrayList);

来对list进行排序

OK 上两张对比图

未整理之前:



整理之后的距离:



文章结束给大家分享下程序员的一些笑话语录:

那是习惯决定的,一直保持一个习惯是不好的!IE6的用户不习惯多标签,但是最终肯定还是得转到多标签的浏览器。历史(软件UI)的进步(改善)不是以个人意志(习惯)为转移的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: