Sorting Your List Screenlet

To sort your list Screenlet, you must point it to a comparator class in your portal. A comparator class implements the logic that sorts your entities. You can create your own comparator class or use those that already exist in your portal. Once your list is sorted, you can split it into sections. This tutorial shows you how to sort your list Screenlet with a comparator and create sections for your sorted list.

First, you’ll learn how to use a comparator to sort your list Screenlet.

Using a Comparator

To use a comparator, you must set the list Screenlet’s obcClassName property to the comparator’s fully qualified class name. Do this in Interface Builder when inserting the Screenlet in an app, just as you would set any other Screenlet property. For example, to set the sample Bookmark List Screenlet to sort its list of bookmarks by URL, you must set Obc Class Name to com.liferay.bookmarks.util.comparator.EntryURLComparator in Interface Builder:

Figure 1: To use a comparator, set the Obc Class Name property in Interface Builder to the comparators fully qualified class name.

Figure 1: To use a comparator, set the *Obc Class Name* property in Interface Builder to the comparator's fully qualified class name.

That’s it! Note that although all list Screenlets inherit the obcClassName property from the BaseListScreenlet class, the list Screenlet must also make its service call with this property. See the Screenlet reference documentation to see which list Screenlets included with Liferay Screens support the obcClassName property. Also, @product@’s comparator classes can change between versions. If you’re using one of these comparators, make sure you specify the one that matches your @product@ version.

Create Sections for Your List

Dividing lists into sections that contain like elements is common in iOS apps. To do this in list Screenlets, first use a comparator to sort the list by the criteria you’ll use to create the sections. Then override the BookmarkListPageLoadInteractor class’s sectionForRowObject method in your list Screenlet’s Interactor. This method is called for each item in the list and should return the information necessary to place the item in a section. For example, the sample Bookmark List Screenlet’s Interactor overrides the sectionForRowObject method to group results by hostname:

public override func sectionForRowObject(object: AnyObject) -> String? {
    guard let bookmark = object as? Bookmark else {
        return nil

    let host = NSURL(string: bookmark.url)?.host?.lowercaseString

    return host?.stringByReplacingOccurrencesOfString("www.", withString: "")

Note that this only produces predictable results when Bookmark List Screenlet is sorted by EntryURLComparator as detailed in the preceding section.

And that’s all there is to it! Now you know how to sort and section your list Screenlet’s list.

Creating iOS List Screenlets

Using Custom Cells with List Screenlets

Creating Complex Lists in Your List Screenlet

iOS Best Practices

« Using Custom Cells with List ScreenletsCreating Complex Lists in Your List Screenlet »