Quantcast
Channel: Kwintessential Notes
Viewing all articles
Browse latest Browse all 305

Sort it out (your JSON collections)

$
0
0

The day after the US elections. Time to sort it out. Not the election results but the collections in the Run4Fun application. I hope you like lists with JSONObjects in your XPages application as much as I do but what you see is normally the order you stick the objects in the list. If you want them to have sorted in a certain way, perhaps by a certain key (firstname, lastname, email, age, time etc) either you have your source (a NotesView?) adapted to it OR you have to apply something that does that to your objects in the list. That something is called a comparator.

A comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of the same class.

I have forgotten to mention a third alternative and that is to sort the collection at display. E.g. the datatables plugin is often used to display Notes data in a way that is similar to a Notes View. The logic of sorting is defined then when initiating the datatable object.

But for the Run4Fun application I am using repeat controls so I have to sort the collections BEFORE I deliver them to the repeat control and frankly the server is quit capable in calculating the sort order for you. Trust me, this comparator is tested on large data-collections. Your Domino server can take it.

So to sort a collection of jsonobjects I need:

  • a collection
  • a key from the jsonobject with which I can compare jsonobjects with
  • a desired order (ascending, descending)

Sorry, no ordering based on multiple keys (yet) but feel free to contribute.

So I added a comparator class:

Which I access from my Utils class:

When I load my objects, e.g. all runners in the application I sort the collection first before returning it. In the example I sort on the “name” key in the json objects and I want the collection in ascending order:

This will return a collection ascending sorted by name:

Okay, the key is hard-coded but I intend to provide a combo-box where the user can select how the results should be returned e.g. by email (ascending), by email (descending), by name (ascending) or by name (descending).

So no need for additional views or additional web plugins to have your data displayed sorted in a specific way!


Viewing all articles
Browse latest Browse all 305

Trending Articles