Liquid Studio 2021
Data Mapping / Data Mapper Functions / By Category / Set / Sort
In This Topic
    Sort
    In This Topic

    Function Name
    Sort
    Category
    Set
    Icon
    Sort
    Description
    Sort input values
    Inputs
    Nodes Nodes to sort
    Sort Key Value to sort nodes on
    Outputs
    Sort Values

    Nodes in sorted order.
    The output takes its type from the input Nodes, see Dynamic Output Values.

    Properties
    None -

    Usage

    The Data Mapper Set Sort function allows you to sort input items on a value of your choice. You can sort items within a specified context, whether the sort key is numerical or textual. To apply the Set Sort function, use the following process:

    Create a new Data Mapper file, dragging your XML data source and targets into the editor area. For this example we are using clubs_source.xsd, and the following XML source:

    Source XML
    Copy Code
    <sport_clubs>
     <club>
      <name>New Town Football Club</name>
      <member>
       <active>Yes</active>
       <member_name>Chris Black</member_name>
       <member_since>2005</member_since>
       <won>League 2009</won>
       <won>Cup 2007</won>
      </member>
      <member>
       <active>Yes</active>
       <member_name>Jim Thomson</member_name>
       <member_since>2003</member_since>
       <won>League 2010</won>
      </member>
      <member>
       <active>No</active>
       <member_name>Barry Jones</member_name>
       <member_since>1996</member_since>
       <won>League 1999</won>
       <won>Cup 1999</won>
       <won>League 2000</won>
      </member>
     </club>
     <club>
      <name>City Netball Team</name>
      <member>
       <active>No</active>
       <member_name>Jacqueline Johnson</member_name>
       <member_since>1999</member_since>
       <won>National Championship 2001</won>
       <won>Regional Championship 2002</won>
       <won>National Championship 2002</won>
      </member>
      <member>
       <active>Yes</active>
       <member_name>Joanne Smith</member_name>
       <member_since>2005</member_since>
       <won>Regional Championship 2007</won>
       <won>National championship 2007</won>
      </member>
      <member>
       <active>Yes</active>
       <member_name>Mary Owens</member_name>
       <member_since>2007</member_since>
       <won>National Championship 2008</won>
      </member>
     </club>
    </sport_clubs>
    

    For our target schema we will be using clubs_target.xsd.

    The data models sporting clubs and their members. Some of the elements can appear multiple times. Let's assume that we want to sort the members in order of when they joined their club. This will involve applying the Sort function to "member" elements using the "member_since" item as Sort Key. To demonstrate the different ways in which you can use the function, we will also sort the "won" elements into alphabetical order within each "member" parent element. Here is the Data Mapper with Source and Target imported:

    Data Mapper Source and Target

    Drag the Set Sort function from the Component Palette into the mapping area:

    Set Sort Component

    Sort Transform Added

    The Set Sort function takes two inputs, representing the item to sort and the key to sort on. The Nodes input should be connected to the source item you want to map in sorted order when output. The Sort Key input should be connected to the value you want the items to be sorted on, which will often be a child element or attribute of the item being sorted. The output connection point will be created when the "Nodes" input connection point is connected.

    Let's start by sorting the members in chronological order of when they joined their clubs. Connect the "Nodes" input of the Sort function to the "member" output in the XML Reader and the "Sort Key" input to the "member_since" output.

    When the Sort components "Nodes" connection point was connected its output connection point "member" will have been created dynamically, we can connect this to the "player" input in the XML Writer. The members will map in order of the year the person joined.

    Reader and Writer to Sort

    Now let's add an additional Sort function to the Mapper to see it used in a slightly different way. Drag another Sort component over and connect both its Nodes and Sort Key inputs to the "won" output in the XML Reader. This will simply sort the "title" elements in the output into alphabetical order within their parent "player" elements.

    Second Sort Function Added

    By connecting both Sort inputs to the same item, we sort the elements using their content values, within their immediate context, i.e. the parent element. With the first Sort function, we used numerical values, so the results will be in ascending numerical order. This time the values are Strings, so the results will be sorted alphabetically.

    Finally, connect the remaining inputs and outputs between Reader and Writer. Remember to map "member_since" in the XML Reader to "joined" in the XML Writer. Although we have used the item as a reference for a Sort function, we have not yet mapped it.

    Inputs and Outputs Connected

    We can now execute the transform by pressing Shift-F5 or the Execute button (Execute). The transform is applied and the file we selected as output opens in the editor:

    Output XML
    Copy Code
    <clubs>
     <team t_name="New Town Football Club">
      <player p_name="Barry Jones" is_active="No">
       <joined>1996</joined>
       <title>Cup 1999</title>
       <title>League 1999</title>
       <title>League 2000</title>
      </player>
      <player p_name="Jim Thomson" is_active="Yes">
       <joined>2003</joined>
       <title>League 2010</title>
      </player>
      <player p_name="Chris Black" is_active="Yes">
       <joined>2005</joined>
       <title>Cup 2007</title>
       <title>League 2009</title>
      </player>
     </team>
     <team t_name="City Netball Team">
      <player p_name="Jacqueline Johnson" is_active="No">
       <joined>1999</joined>
       <title>National Championship 2001</title>
       <title>National Championship 2002</title>
       <title>Regional Championship 2002</title>
      </player>
      <player p_name="Joanne Smith" is_active="Yes">
       <joined>2005</joined>
       <title>National championship 2007</title>
       <title>Regional Championship 2007</title>
      </player>
      <player p_name="Mary Owens" is_active="Yes">
       <joined>2007</joined>
       <title>National Championship 2008</title>
      </player>
     </team>
    </clubs>
    

    The "player" items appear within their "team" parent elements in order of "joined" dates. The "title" elements also appear in alphabetical order within their parent "player" elements.

    As with many of the Mapping functions, Sort can prove extremely useful when used in conjunction with other functions from the Set, Comparator and Aggregate sections.
    See Also