Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9782

Can I sort cells in Google Sheets with a key function?

$
0
0

I have some data in a Google Sheets document that I would like to sort by computing some value from two columns and using that as the sort key.

Say my data is like this:

ABCD
1......87005
2......132000
3......04200
4......25007000

I want to sort the data in decending order based on max(C, D) so that the data looks like this:

ABCD
1......132000
2......87005
3......25007000
4......04200

I was able to do it with a Python script like this, but that required exporting the sheet and then re-importing it.

import csvwith open('source_sheet.csv', newline='') as in_file, open('output.csv', 'w', newline='') as out_file:    reader = csv.DictReader(in_file)    sorted_data = sorted(reader, key=lambda x: max(int(x['C']), int(x['D']), reverse=True)    writer = csv.DictWriter(out_file, fieldnames=reader.fieldnames)    writer.writeheader()    writer.writerows(sorted_data)

Is there some formula or function that can do what I want?


Viewing all articles
Browse latest Browse all 9782

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>