As mentioned in Latex hyperref and cite packages, packages cite and natbib are used to sort numerical citations. The usage of cite is
\usepackage{cite}
It is quite simple because you don't need to do anything more. [21,18,22,23] will be automatically sorted and compressed as [18, 21-23].
To get the same results, options needed to be provided using natbib:
\usepackage[options]{natbib}
Options can be sort&compress or compress. The former one sorts and compresses the citations and the latter one compresses the citations only. Because of the compatible issue, natbib is recommended.