ishandutta2007
ishandutta2007

Reputation: 18274

How to refactor the code to make each of lists in source code sorted?

To be clear, I'm not asking how to sort the lists at runtime. I want to alter the source code.

Essentially I want to refactor the following code with as little effort as possible. I have lots of hardcoded long lists across the codebase, so to sort them manually is tedious,

One option I can think of is to encapsulate all list in code with Sorted.

t = ["therock","hollywood","rockbottom","inyourhouse"]
Sorted([t])

But I am looking for ways to do it without adding addition code cycle, options like running other script or via code editor extension or by any command on top of the codebase to auto-format it.

t = ["therock","hollywood","rockbottom","inyourhouse"]

should become

t = ["hollywood","inyourhouse","rockbottom","therock"]

Upvotes: 0

Views: 185

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37187

It may be too late for your problem, but I usually like to write list literals like this:

t = [
    'therock',
    'hollywood',
    'rockbottom',
    'inyourhouse'
]

I think this makes it a bit easier to distinguish items at a glance, and it also lends itself well to line-based text manipulation in an editor. For example, using vim, I would sort the lines by selecting them in visual mode and using :sort.

Upvotes: 1

Related Questions