Sort a List Map in Sketchware

To sort a List Map we can use a Comparator to compare the values of keys in the List Map and then sort according to it.

1. Suppose you have a ListMap maplist which is displayed in custom ListView listview1.

2. Create a more block sort [List Map: mylist] accordingToKey [key]. Put following codes in it.

Comparator list_sorter = new Comparator<HashMap<String, Object>>(){

@Override

public int compare(HashMap<String, Object> map1, HashMap<String, Object> map2){

try {

Double num1 = Double.parseDouble(map1.get(_key).toString());

Double num2 = Double.parseDouble(map2.get(_key).toString());

return Double.compare(num1, num2);

} catch (NumberFormatException e){

return map1.get(_key).toString().compareTo(map2.get(_key).toString());

}

}

};

Collections.sort(_mylist, list_sorter);


3. Use the block where you want to sort the List Map. The block should be followed by ListView setListCustomViewData block for displaying the sorted List Map.

The blocks in above image will sort maplist according to key totalConfirmed and display it in listview1.

4. To sort in reverse order or descending order, the code used in the more block should be following.

Comparator list_sorter = new Comparator<HashMap<String, Object>>(){

@Override

public int compare(HashMap<String, Object> map1, HashMap<String, Object> map2){

try {

Double num1 = Double.parseDouble(map1.get(_key).toString());

Double num2 = Double.parseDouble(map2.get(_key).toString());

return Double.compare(num2, num1);

} catch (NumberFormatException e){

return map2.get(_key).toString().compareTo(map1.get(_key).toString());

}

}

};

Collections.sort(_mylist, list_sorter);


Watch video below :


Comments

  1. bro !how to build edittext search listview .
    I make dictionary .please help me.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create Music Player app in Sketchware

A Flash Light App in Sketchware

Admob rewarded video Ads in Sketchware

Firebase auth in Sketchware for Login Activity

Simple car racing android game in Sketchware