0

I am interested in creating a table with positive Gaussian integers listed in order based on their norms, with ties broken by lex order.

So far I have Sort[Flatten[Table[{a, b, a^2 + b^2}, {a, 0, 10}, {b, 0, 10}], 1], #1[[3]] < #2[[3]] &], 1], which gives me some Gaussian integers sorted by norm, but the ties are broken in revlex.

For example, the code gives {1,0,1},{0,1,1} as the first nonzero entries, but I want those to be reversed. A second application of Sort destroys the first Sort, so I need to be more clever, and alas I am not.

Any help would be appreciated.

Trevor
  • 161
  • 1

1 Answers1

1

Your code has syntax errors but I think this is what you want:

list = Flatten[Table[{a, b, a^2 + b^2}, {a, 0, 10}, {b, 0, 10}], 1];

SortBy[list, #[[3]] &]

{{0, 0, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 2}, {0, 2, 4}, {2, 0, 4}, {1, 2, 5}, {2, 1, 5}, {2, 2, 8}, {0, 3, 9}, {3, 0, 9}, {1, 3, 10}, {3, 1, 10}, {2, 3, 13}, {3, 2, 13}, {0, 4, 16}, {4, 0, 16}, {1, 4, 17}, {4, 1, 17}, {3, 3, 18}, {2, 4, 20}, {4, 2, 20}, {0, 5, 25}, {3, 4, 25}, {4, 3, 25}, {5, 0, 25}, {1, 5, 26}, . . .}

With this syntax ties are (automatically) broken using the default ordering function.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • That certainly does what I was hoping for. I just wish I understood the inner workings. I use Mathematica so rarely that I lose anything I learn during the few times a year I use it. Thanks. – Trevor Jan 15 '15 at 19:47
  • @Trevor Please read the documentation for SortBy. If you still have questions post them here as comments and I shall try to answer them later today. – Mr.Wizard Jan 15 '15 at 19:48