简体   繁体   中英

Sort django query set based on orders in a list

I have a django query set like:

the_query_set = {<obj1>, <obj2>, <obj3>}

and a corresponding list:

the_list = [False, True, True]

how can I sort the_query_set in order of sorted list:

the_sorted_list = [True, True, False]
desired_sorted_query_set = {<obj2>, <obj3>, <obj1>}

This could be a solution:

[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0])]

or

[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0], reverse=True)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM