When changing sort order by invoice_number in descending, it counts 9 then 2 then 10 which is not correct

Hi there,

I followed the instructions on [Solved] Strange invoice order in order to re-order the Invoices how I’d like. I chose by invoice number. So the line in my file looks like this: $this->db->order_by(‘ip_invoices.invoice_number DESC’);

This has worked well except this is the first time I’ve gotten into an issue where it seems that the system sees 10 as a lower number than 9 (presumably only looking at the number one). Here is a screenshot showing it working well except notice the top three lines not in order:

I believe this is a bug as it should order it like this in descending order:


Does that make sense? Is this a bug or expected behaviour?

Why did you changed this to the number? Because normally invoices are ordered by their internal database id which would solve the problem with invoice numbering.

This works without problems:
$this->db->order_by('ip_invoices.invoice_id DESC');

Hi @kovah,

The reason I changed it was because I had created a few normal invoices in the system and then began to import my invoices from another system which I have done successfully. The problem is that it shows the new invoices on top of my old ones and in no particular order other than how they were imported. I wanted my most recent at the top so I chose invoice number and that solved the problem until yesterday when I noticed this odd bug.

I have since changed it to be sorted by the created date and that seems to have solved the problem for me now. But it only solves the ordering for me, the issue I found still remains that the sorting isn’t counting them the way a human would expect. It’s counting them based on each individual number which is why it saw 10 as lower than 9 because the 10 starts with 1. I believe this behaviour should be changed.


This is lexographic sorting which means it is sorting the element as a string not a number and in my experience is how computers usually sort numbers. It wasn’t until Windows 7 I think that Windows Explorer sorted numbers they way we expect them to be sorted. All “sorted by name” file lists would sort like this:

DOS dir:
10/01/2016 02:36 PM 0 20151210.txt
10/01/2016 02:35 PM 0 2015128.txt
10/01/2016 02:35 PM 0 2015129.txt

Linux ls:
-rw-r–r-- 1 catfish catfish 6 Jan 10 14:38 20151210.txt
-rw-r–r-- 1 catfish catfish 6 Jan 10 14:38 2015128.txt
-rw-r–r-- 1 catfish catfish 6 Jan 10 14:38 2015129.txt

See http://programmers.stackexchange.com/questions/127639/why-do-some-sorting-methods-sort-by-1-10-2-3 for more info.

The quickest fix or workaround for this is to use 01, 02, 03 for 1, 2, 3 etc. So you have 20151208 20151209 20151210

That said, it would be nice to have a ‘sort by’ function in InvoicePlane like we usually have when we look at rows and columns of data, like clicking the column heading to sort by that column, click again to reverse sort order. I’d guess there is already a request for it.

1 Like