I have database of cards that has a Number field which represents the number of the card in the set. This field is a CharField as some of the cards have their number followed by a symbol to represent different versions of the same card. I would like to order my data using this field however it orders weird due to the field being a string. For example; 1, 10, 100, 101, 102...
code:
def cards_page(request):
card_list = Card.objects.exclude(side='b').extra(select={'int': 'CAST(number AS INTEGER)'}).order_by('int')
The code above falls over due to the symbols in this field. I read around that using the below regex code snippet will remove the non numeric characters but cannot seem to get it to work in the select.
"REGEXP_REPLACE(number, '[^0-9.]', '')"
Is there a way to remove the non numeric character before casting to an integer and ordering by that value?