# How to Sort an Integer in Python Let’s say that you have an integer with value of `9271` and you want to sort it either ascending(`1279`) or descending(`9721`).

How would you do this in Python? How would you sort an integer in either ascending or descending order?

## Sorting our integer

Let’s set our integer to `n`:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
`n = 9271`Code language: Python (python)```

Now let’s sort it:

``````sorted([i for i in str(n)])

# This will give us a list in ascending order
# ['1', '2', '7', '9']```Code language: Python (python)```

We can as easily do it the other way:

``````sorted([i for i in str(n)], reverse=True)

# This will give us a list in descending order
# ['9', '7', '2', '1']```Code language: Python (python)```

## How do we convert our sorted list back to an integer?

``````s = sorted([i for i in str(n)], reverse=True)
int("".join(s))

# 9721
```Code language: Python (python)```

But can we do this in a single line? Yes!

``````descending = int("".join(sorted([i for i in str(n)], reverse=True)))

print(descending)

# 9721
```Code language: Python (python)```

If you just want a quick copy/paste, then you can use these functions to sort an integer in python:

``````def sort_asc(n):
return int("".join(sorted([i for i in str(n)])))

def sort_desc(n):
return int("".join(sorted([i for i in str(n)], reverse=True)))
```Code language: Python (python)```

And here is how to use these functions in how to sort an integer using python:

``````print(sort_asc(473829))
# 234789

print(sort_desc(473829))
# 987432
```Code language: Python (python)```
``````def sort_asc(n):
return int("".join(sorted([i for i in str(n)])))

def sort_desc(n):
return int("".join(sorted([i for i in str(n)], reverse=True)))

print(sort_asc(473829))
# 234789

print(sort_desc(473829))
# 987432
```Code language: Python (python)```

Tags:
Subscribe
Notify of Inline Feedbacks TheNewCoder
5 months ago

a nice blog. it’s clearly understand. Thanks for your effort. Mohammed Muzammil
1 year ago

how to find the len of an integer
for eg:
n = 98743
len of n is 5
but int type doesn’t support len function Mohammed Muzammil
1 year ago

thank you Coen de Groot
1 year ago

For a slightly simpler version, sorted(str(n)) also works. A string is also an iterable

5
0