Sort Firebase data for Android project

If your firebase data contains same keys at each position, as shown in image below,


then you can follow the steps below for sorting the data before displaying it in an app created in Sketchware.

1. In your Sketchware project, add a ListView listview1.

2. Create a CustomView custom.xml. In this View add TextViews for displaying data at each position in Firebase database.

3. In properties of listview1 select custom.xml as customView.

4. Add a new List Map maplist.

5. Add a FirebaseDB component with any name, and with data location same as in your Firebase database. (fdb: results)

6. Add ListView onBindCustomView event. In this event use blocks for displaying the data from List Map maplist in ListView listview1.
7. Save and run the project.

8. Delete the installed app.

9. Export source code of this project. Unzip it in a folder on sdcard.

10. Open the project in AIDE.

11. In app level build.gradle file, in dependencies, change all 'implementation' to 'compile' and change the version of all these libraries to '+'. After that save the file. It will download the required libraries.

12. Now open MainActivity.java, and find the code
private void initializeLogic(){
}

Inside this put the following code:
com.google.firebase.database.Query query = fdb.orderByChild("name");

ValueEventListener _valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try { maplist = new ArrayList<>();
for (DataSnapshot _data : dataSnapshot.getChildren()) {
HashMap<String, Object> _map = _data.getValue(new GenericTypeIndicator<HashMap< String, Object>>() {});
maplist.add(_map);
}
listview1.setAdapter(new Listview1Adapter(maplist));
((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged(); 
} catch (Exception e){
showMessage(e.toString()); }
}
@Override
public void onCancelled(DatabaseError databaseError) { }
};

query.addValueEventListener(_valueEventListener);

Here fdb is FirebaseDB component and name is the key according to which data is to be sorted alphabetically and numerically.

13. Save and run the project. The app will display the data sorted alphabetically for key 'name'. To sort according to any other key, just change the key in the code.

Watch the video below

https://youtu.be/-uwuzcjabQw