+ 6
How can I sort odd numbers to come first followed by even numbers
I want to sort in that, this input:[5,1,3,9,2,6,4,7,8] would result to:[1,3,5,7,9,2,4,6, 8] ::odds sorted, then followed by the evens. I code in python and Javascript, so I would prefer theuse of those two languages, but you can also give an answer in any other language.
4 Answers
+ 16
š¬š¬š¬š¬
https://code.sololearn.com/WGo95FDvsa3f/?ref=app
+ 10
I madr a simple code in Python:
https://code.sololearn.com/cRG5Zt1iZ3n3/?ref=app
+ 5
I'm not a python programmer, but you can try a bubble sort, with different criteria this time around (i.e. if |number[n]| % 2 > |number[n + 1]| % 2 then swap them)
0
Python is my next goal... for now, with Ruby, I can suggest this (I am sure a Python or JavaScript solution would be quite similar):
numsĀ =Ā [5,1,3,9,2,6,4,7,8]
even_numsĀ =Ā nums.selectĀ {Ā |x|Ā (xĀ %Ā 2)Ā ==Ā 0Ā }
odd_numsĀ =Ā nums.selectĀ {Ā |x|Ā (xĀ %Ā 2)Ā !=Ā 0Ā }
sorted_numsĀ =Ā odd_nums.sortĀ +Ā even_nums.sort
print sorted_nums
(using modulo 2 to identify odds and evens, selecting on that criteria, recombining after sorting)