Ordering by specific field value first pyspark

Viewed 154
id | name | priority
--------------------
 1 | core  |   10   
 2 | core  |   9    
 3 | other |   8    
 4 | board |   7    
 5 | board |   6    
 6 | core  |   4    

I want to order the result set using priority but first those rows that have name=core even if have lower priority. The result should look like this

id | name | priority
--------------------
 6 | core  |   4    
 2 | core  |   9    
 1 | core  |   10   
 5 | board |   6    
 4 | board |   7    
 3 | other |   8    
1 Answers

You can order by a boolean that checks whether the name is equal to core:

import pyspark.sql.functions as F

df.orderBy(F.col('name') != 'core', 'priority').show()
+---+-----+--------+
| id| name|priority|
+---+-----+--------+
|  6| core|       4|
|  2| core|       9|
|  1| core|      10|
|  5|board|       6|
|  4|board|       7|
|  3|other|       8|
+---+-----+--------+
Related