Sorting

From Apache OpenOffice Wiki
Jump to: navigation, search



Table Sort Descriptor

A sort descriptor describes all properties of a sort operation. The service com.sun.star.table.TableSortDescriptor2 extends the service com.sun.star.util.SortDescriptor2 with table specific sorting properties, such as:

The sorting orientation using the boolean IsSortColumns.

A sequence of sorting fields using the SortFields property that contains a sequence of com.sun.star.table.TableSortField structs.

The size of the sequence using the MaxSortFieldsCount property.

The service com.sun.star.sheet.SheetSortDescriptor2 extends the service com.sun.star.table.TableSortDescriptor2with spreadsheet specific sorting properties, such as:

Moving cell formats with the cells they belong to using the boolean property BindFormatsToContent.The existence of column or row headers using the boolean property ContainsHeader.

Copying the sorted data to another position in the document using the boolean property CopyOutputData.

Position where sorted data are to be copied using the property OutputPosition.

If the IsUserListEnabled property is true, a user-defined sort list is used that specifies an order for the strings it contains. The UserListIndex property selects an entry from the UserLists property of the com.sun.star.sheet.GlobalSheetSettings service to find the sort list that is used.

SheetSortDescriptor2

To sort the contents of a cell range, the sort() method from the com.sun.star.util.XSortable interface is called, passing a sequence of property values with properties from the com.sun.star.sheet.SheetSortDescriptor2 service. The sequence can be constructed from scratch containing the properties that should be set, or the return value of the createSortDescriptor() method can be used and modified. If the cell range is a database range that has a stored sort operation, createSortDescriptor() returns a sequence with the options of this sort operation.

The fields that the cell range is sorted by are specified in the SortFields property as a sequence of com.sun.star.table.TableSortField elements. In the com.sun.star.table.TableSortField struct, the Field member specifies the field number by which to sort, and the boolean IsAscending member switches between ascending and descending sorting for that field. The boolean IsCaseSensitive specifies whether the case of letters is important when comparing entries. The CollatorLocale is used to sort according to the sorting rules of a given locale. For some locales, several different sorting rules exist. In this case, the CollatorAlgorithm is used to select one of the sorting rules. The com.sun.star.i18n.Collator service is used to find the possible CollatorAlgorithm values for a locale. Currently, it is not possible to have different locales, algorithms and case sensitivity on the different fields.

Documentation note.png The FieldType member, that is used to select textual or numeric sorting in text documents is ignored in the spreadsheet application. In a spreadsheet, a cell always has a known type of text or value, which is used for sorting, with numbers sorted before text cells.

The following example sorts the cell range by the second column in ascending order:

  // --- sort by second column, ascending ---
 
  // define the fields to sort
  com.sun.star.util.SortField[] aSortFields = new com.sun.star.table.TableSortField[1];
  aSortFields[0] = new com.sun.star.table.TableSortField();
  aSortFields[0].Field = 1;
  aSortFields[0].IsAscending = true;
  aSortFields[0].IsCaseSensitive = false;
 
  // define the sort descriptor
  com.sun.star.beans.PropertyValue[] aSortDesc = new com.sun.star.beans.PropertyValue[2];
  aSortDesc[0] = new com.sun.star.beans.PropertyValue();
  aSortDesc[0].Name = "SortFields";
  aSortDesc[0].Value = aSortFields;
  aSortDesc[1] = new com.sun.star.beans.PropertyValue();
  aSortDesc[1].Name = "ContainsHeader";
  aSortDesc[1].Value = new Boolean(true);
 
  // perform the sorting
  com.sun.star.util.XSortable xSort = (com.sun.star.util.XSortable)
      UnoRuntime.queryInterface(com.sun.star.util.XSortable.class, xRange);
  xSort.sort(aSortDesc);
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages