add

open fun add(item: T): Int(source)

Adds the given item to the list. If this is a new item, SortedList calls onInserted.

If the item already exists in the list and its sorting criteria is not changed, it is replaced with the existing Item. SortedList uses areItemsTheSame to check if two items are the same item and uses areContentsTheSame to decide whether it should call onChanged or not. In both cases, it always removes the reference to the old item and puts the new item into the backing array even if areContentsTheSame returns false.

If the sorting criteria of the item is changed, SortedList won't be able to find its duplicate in the list which will result in having a duplicate of the Item in the list. If you need to update sorting criteria of an item that already exists in the list, use updateItemAt. You can find the index of the item using indexOf before you update the object.

Return

The index of the newly added item.

Parameters

item

The item to be added into the list.

See also