Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space DEV and version r0682

D toc

D s transforms

Excerpt

Sorts the dataset based on one or more columns in ascending or descending order. You can also sort based on the order of rows when the dataset was created. 

Limitations:

Info

NOTE: This transform is intended primarily for use in the Transformer page. Sort order may not be preserved in the output files.

  • If you generate a new sample after a sort transform has been applied, the sort order is not retained. You can re-apply the sort step, although the following limitations still apply.
  • Sort order is not preserved on output when the output is a multi-part file.

D s
snippetBasic

D code
sort order:LastName

Output: Dataset is sorted in alphabetically ascending order based on the values in the LastName column, assuming that the values are strings.

D s
snippetParameters

D code

sort order:column_ref

TokenRequired?Data TypeDescription
sortYtransformName of the transform
orderYstringName of column or columns by which to sort

D s lang notes

order

Include Page
order_sort Parameter
order_sort Parameter

D s
snippetusage

Required?Data Type
YesString (column name)

Data is sorted based on the data type of the source:   

Data Type of SourceSort Order
IntegerNumerical
DecimalNumerical
DatetimeNumerical
All othersString

D s
snippetExamples

Example - sort methods

Source:

The column without a name identifies the original row numbers. In the data grid, this information is available when you hover over the black dot to the left of a row of data. 

 CustIdFirstNameLastNameCityStateLastOrder
11001SkipJonesSan FranciscoCA25
21002AdamAllenOaklandCA1099
31003DavidWigginsOaklandMI125.25
41004AmandaGreenDetroitMI452.5
51005ColonelMustardLos AngelesCA950
61006PaulineHallSaginawMI432.22
71007SarahMillerCheyenneWY724.22
81008TeddySmithJuneauAK852.11
91009JoelleHigginsSacramentoCA100

Transformation:

First, you might want to clean up the number formatting in the lastOrder column. The following formats the values to always include two digits after the decimal point:

D trans
RawWrangletrue
Typestep
WrangleTextset col:LastOrder value:numformat(LastOrder, '####.00')
p01NameColumns
p01ValueLastOrder
p02NameFormula
p02Valuenumformat(LastOrder, '####.00')
SearchTermEdit column with formula

Now, you're interested in the highest value for your customers' most recent orders. You can apply the following sort:

D trans
RawWrangletrue
Typestep
WrangleTextsort order: -LastOrder
p01NameSort by
p01Value-LastOrder
SearchTermSort rows

Rows are sorted by the LastOrder column in descending order (largest to smallest):

 CustIdFirstNameLastNameCityStateLastOrder
21002AdamAllenOaklandCA1099.00
51005ColonelMustardLos AngelesCA950.00
81008TeddySmithJuneauAK852.11
71007SarahMillerCheyenneWY724.22
41004AmandaGreenDetroitMI452.50
61006PaulineHallSaginawMI432.22
31003DavidWigginsOaklandMI125.25
91009JoelleHigginsSacramentoCA100.00
11001SkipJonesSan FranciscoCA25.00

The above row numbers represent the original order of the rows. Now, you want to get your data geographically organized by sorting by city and state. You can perform multi-column sorts such as the following, which sorts first by State and then by City columns:

D trans
RawWrangletrue
Typestep
WrangleTextsort order: State,City
p01NameSort by
p01ValueState,City
SearchTermSort rows

In the generated output, the data is first sorted by the State value. Each set of rows within the same State value is also sorted by the City value.

 CustIdFirstNameLastNameCityStateLastOrder
81008TeddySmithJuneauAK852.11
51005ColonelMustardLos AngelesCA950.00
21002AdamAllenOaklandCA1099.00
91009JoelleHigginsSacramentoCA100.00
11001SkipJonesSan FranciscoCA25.00
41004AmandaGreenDetroitMI452.50
31003DavidWigginsOaklandMI125.25
61006PaulineHallSaginawMI432.22
71007SarahMillerCheyenneWY724.22

Example - Sort by original row numbers

Include Page
EXAMPLE - SOURCEROWNUMBER Function
EXAMPLE - SOURCEROWNUMBER Function

D s also
labelwrangle_transform_sort