我想要对基于键的值进行排序所以我使用了TreeMap,它给出了排序键,但我想根据键对键和值进行排序。 这里键为Double,值为Array-list。
MainActivity.java
public void getExpandableListData() {
Cursor cursor = databaseHelper.getLoadMoreData(count);
cursor.moveToFirst();
String businessName;
String latitude;
String longitude;
String categoryDescription;
double difference;
do {
TreeMap<Double, List<ChildInfo>> treeMapLocationDifference;
treeMapLocationDifference = new TreeMap<Double, List<ChildInfo>>();
categoryDescription = cursor.getString(cursor
.getColumnIndex("categorydesc"));
int categoryId = cursor.getInt(cursor.getColumnIndex("CategoryId"));
listDataHeader.add(categoryDescription);
List<ChildInfo> childList = new ArrayList<ChildInfo>();
Cursor cursorChild = databaseHelper.getChildData(categoryId);
Map.Entry<Double, List<ChildInfo>> entry;
List<ChildInfo> list = null;
cursorChild.moveToFirst();
do {
businessName = cursorChild.getString(cursorChild
.getColumnIndex("BusinessName"));
phoneNumber = cursorChild.getString(cursorChild
.getColumnIndex("Phone"));
categoryDescription = cursorChild.getString(cursorChild
.getColumnIndex("categorydesc"));
latitude = cursorChild.getString(cursorChild
.getColumnIndex("Latitude"));
longitude = cursorChild.getString(cursorChild
.getColumnIndex("Longitude"));
ChildInfo childInfo = new ChildInfo(businessName, phoneNumber,
categoryDescription, latitude, longitude);
childList.add(childInfo);
currentLocation = new Location("");
currentLocation.setLatitude(18.5522);
currentLocation.setLongitude(73.8956);
Location savedLocation = new Location("databaseLocation");
savedLocation.setLatitude(Double.parseDouble(latitude));
savedLocation.setLongitude(Double.parseDouble(longitude));
difference = currentLocation.distanceTo(savedLocation) * (0.001);
treeMapLocationDifference.put(difference, childList);
entry = treeMapLocationDifference.entrySet().iterator().next();
list = new ArrayList<ChildInfo>();
list = entry.getValue();
listDataChild.put(categoryDescription, list);
} while (cursorChild.moveToNext());
} while (cursor.moveToNext());
cursor.close();
}
这里我使用了Tree-map,我希望这个按照键和它对应的值排序的树图,然后我将这个值赋给hash-map的值,该值应该被排序。