orderBy和sort:按指定字段排序,默认为升序 ,例如,按照时间字段进行排序:
df.orderBy("visittime").show(false) 输出: +---+----+----+---------------------+ |id |user|type|visittime | +---+----+----+---------------------+ |12 |1 |助手2 |2017-07-07 13:45:08.0| |10 |2 |助手1 |2017-07-14 13:44:57.0| |11 |1 |APP1|2017-07-15 13:45:03.0| |6 |3 |APP1|2017-08-01 13:44:41.0| |5 |3 |APP1|2017-08-02 13:44:38.0| |8 |3 |APP2|2017-08-03 13:44:45.0| |2 |1 |APP1|2017-08-04 13:44:26.0| |3 |2 |助手1 |2017-08-05 13:44:29.0| |4 |2 |助手1 |2017-08-07 13:44:32.0| |1 |1 |助手1 |2017-08-10 13:44:19.0| |9 |2 |APP2|2017-08-11 13:44:53.0| |7 |3 |助手2 |2017-08-14 13:44:48.0| +---+----+----+---------------------+如果想要降序排序,可以使用如下的方法:
df.orderBy(df("visittime").desc).show(false) 输出: +---+----+----+---------------------+ |id |user|type|visittime | +---+----+----+---------------------+ |7 |3 |助手2 |2017-08-14 13:44:48.0| |9 |2 |APP2|2017-08-11 13:44:53.0| |1 |1 |助手1 |2017-08-10 13:44:19.0| |4 |2 |助手1 |2017-08-07 13:44:32.0| |3 |2 |助手1 |2017-08-05 13:44:29.0| |2 |1 |APP1|2017-08-04 13:44:26.0| |8 |3 |APP2|2017-08-03 13:44:45.0| |5 |3 |APP1|2017-08-02 13:44:38.0| |6 |3 |APP1|2017-08-01 13:44:41.0| |11 |1 |APP1|2017-07-15 13:45:03.0| |10 |2 |助手1 |2017-07-14 13:44:57.0| |12 |1 |助手2 |2017-07-07 13:45:08.0| +---+----+----+---------------------+