7.2 Sort the structured data

 

7.1 Sort a sequence


1. Single-field sorting

2. Multi-field sorting

3. Position after sorting

4. Random sorting

SPL

A B
1 =file(“EMPLOYEE.csv”).import@tc()
2 =A1.sort(BIRTHDAY) /Sort by birthday in ascending order
3 =A1.sort([BIRTHDAY,-HIREDATE]) /Sort by birthday in ascending order and by hire date in descending order
4 =A1.psort@z(SALARY) /Position after sorting by salary in descending order
5 =A1.sort(rand()) /Random sorting

SQL

1. Single-field sorting

SELECT *
FROM EMPLOYEE
ORDER BY BIRTHDAY;

2. Multi-field sorting

SELECT *
FROM EMPLOYEE
ORDER BY BIRTHDAY ASC,HIREDATE DESC;

3. Position after sorting

SELECT rn,SALARY
FROM(SELECT EMP.*,ROWNUM AS rn
    FROM (SELECT * FROM EMPLOYEE ORDER BY EID) EMP)
ORDER BY SALARY DESC;

4. Random sorting

SELECT *
FROM EMPLOYEE
ORDER BY dbms_random.value;

Python

df = pd.read_csv('../EMPLOYEE.csv')
#Sort by birthday in ascending order
sorted_by_birthday = df.sort_values('BIRTHDAY') 
#Sort by birthday in ascending order and by hire date in descending order
sorted_by_birthday_hiredate = df.sort_values(['BIRTHDAY', 'HIREDATE'], ascending=[True, False])
#Position after sorting by salary in descending order
sorted_positions = np.argsort(-df['SALARY'].values)
#Random sorting
random_sorted = df.sample(frac=1, random_state=42)

7.3 Rank a sequence
Example codes for comparing SPL, SQL, and Python