Working with Data: All the calculations you need t
  • Introduction
  • What is this book?
  • How to Use this Book
  • Document Notes
  • Authors' Thanks
  • Strings
    • Concatenate
    • Split
    • Length
    • Slice
    • Trim
    • Contains
    • Match
    • Replace
    • Upper/Lower Case
  • Numbers
    • Sum
    • Average
    • Roll Ups
    • Round
    • Floor
    • Power
    • Square Root
    • Absolute
  • Dates
    • Date Difference
    • Age
  • Logical
    • Logical Operators
    • If
    • Case / Switch
    • Is Null/Empty
  • General
    • Count
    • Filter
    • Sort
    • Lookup/Join
    • Variables
    • Convert Type
  • Appendix
    • DateTime Parts
    • Helpful Resources
Powered by GitBook
On this page
  • Excel
  • Tableau
  • Alteryx
  • OrgVue
  • tSQL
  • Python

Was this helpful?

  1. General

Sort

PreviousFilterNextLookup/Join

Last updated 5 years ago

Was this helpful?

Excel

Select a range of cells > go to the Home tab > Editing > Sort & Filter and then select sorting options.

Tableau

Rank can be added as a table calculation or with the syntax: ~RANK!~(‹field› «, 'direction'») where «direction» can be either asc or desc.

RANK([Country], 'asc')
RANK([Salary], 'desc')

Alteryx

Use the Sort tool (in the Preparation palette).

(and select the Sort and Direction values).

OrgVue

In OrgVue, strings and numbers are sorted differently. Sorting strings: array(‹string_array›).~sort!~('item'«, 'direction'») OR ‹collection›.~sort!~('‹dimension›'«, 'direction'»)

array([2,1,'b','c','a']).sort('item', 'desc'); // returns c,b,a,2,1
// OR
node.d.sort('department', 'asc');

NB.:

  • If no direction is supplied, an ascending order is applied by default.

  • If the values being sorted are of mixed type, sort() lists numbers in ascending order and then sorts strings alphabetically (which can be considered 'ascending order').

Sorting numbers:

It is possible to apply the above syntax when sorting a measure. However, it will treat the measure as if it were a string, e.g. sorting 9.9 -> 800 -> 70 -> ... To correctly sort numerical fields, use: ‹collection›.~sort!~('‹measure›', ~sort.number!~|sort.‹direction›)

node.c.sort('current_salary', sort.number|sort.desc)

tSQL

~ORDER BY!~ ‹field› [‹direction›]

SELECT * 
FROM EmployeeData
ORDER BY CurrentSalary [DESC]

Python

~sorted!~(‹arr›, ‹key=lambda n: n.name›)

sorted(arr, key=lambda n: n.name)

This assumes that the variable arr is an array containing objects. If you want to sort a simple array of strings, you can use: ~sorted!~(‹array›, «reverse=True») where the inclusion of reverse=True reverses the sort order:

sorted(['2','1','b','c','a']) 
# Returns ['1', '2', 'a', 'b', 'c']

sorted(['2','1','b','c','a'], reverse=True) 
# Returns ['c', 'b' , 'a', '2', '1']

NB.:

  • An ascending order is applied by default.

  • If the values being sorted are of mixed type, sorted() lists numbers in ascending order and then sorts strings alphabetically (which can be considered 'ascending order').

  • All numbers must be wrapped in quotes.