# Ordered list of integer vectors

For a given length $$n$$ and a maximum positive integer value $$N$$, I'd like to generate a list that contains all $$n$$-vectors with entries between $$-N$$ and $$N$$ (of which there are $$(2N+1)^n$$. Further, the list should be sorted in such a way that first the element with all zeroes appears, then the elements with a single non-zero entry, then two non-zero entries, and so on. How the elements are ordered within a block with a specific amount of non-zero entries is unimportant.

• What have you tried so far? Generally, people here appreciate if questions show a minimum level of effort on the askers part. To get you started, take a look at Range, Tuples, SortBy and Count. Aug 9, 2022 at 10:08
• You surely mean $(2N+1)^n$ not $n^{2N+1}$? Aug 9, 2022 at 10:10
• @yarchik indeed, thanks, editied Aug 9, 2022 at 10:12
• @LukasLang I'm stuck in the stage of confusion. I will look at the functions you mention Aug 9, 2022 at 10:14
• Try to sort Table[IntegerDigits[i, k, n] - ConstantArray[1, n], {i, 0, k^n - 1}]. Aug 9, 2022 at 10:22

ReverseSortBy[Tuples[Range[-nmax, nmax], n], Count[#, 0] &]

does the job. Here nmax = $$N$$ from the question.
• This is almost exactly what I had in mind. One minor improvement: You can write Count instead of Count[#, 0]& (this is the third usage form in the documentation of Count) Aug 9, 2022 at 10:25