w00t w00t! It's Monday! Today's problem may be a bit challenging but hopefully we have some interesting solutions.
Given a 2 dimensional array, sort the arrays by a chosen column number. For example:
//sort(array, 0) [[12, AAA], [18, DDD], [28, CCC], [58, BBB]] //sort(array, 1) [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'], [18, 'DDD']]
Comments:
Anonymous - 10 years, 6 months ago
How can array hold different data types?
reply permalink
David - 10 years, 6 months ago
In the context of C based languages, it could be a void array or it could involve some kind of struct. You could also treat all numbers as strings, ordering letters and numbers based on their value in ASCII or Unicode.
In python, you can set up an array of pairs. I think it's called a Tuple. I'm not sure if you can use the built in sort method on that for a selected side of the pair, but at least you can write a manual sort that will do that. I assume other, similar high level/scripting languages will have a similar enough feature.
reply permalink
Mre64 - 10 years, 6 months ago
thank you, that helps a lot.
reply permalink
Mre64 - 10 years, 6 months ago
/* Mre64 6/23/14
*/ import java.util.Arrays;
public class Array2DSort { //initial 2d array, and a swap array static int[][] sort = new int[5][2]; static int[] list = new int[5];
}
reply permalink
Mre64 - 10 years, 6 months ago
I just went with the spirit of the solution rather than dealing with the multi-type array issue. -mre
reply permalink
Max Burstein - 10 years, 6 months ago
If it helps you out any you can convert the ints to char arrays and do the problem like that. I realize that can be kind of annoying for statically typed languages.
reply permalink
Anonymous - 10 years, 6 months ago
reply permalink