Problem of the Day
A new programming or logic puzzle every Mon-Fri

Double Sort

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']]

Permalink: http://problemotd.com/problem/double-sort/

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

    ''I was recently on a tour of Latin America, and the only regret I have was that I didn't 
      study Latin harder in school so I could converse with those people.''
        —Dan Quayle
    

    */ 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];

    public static void main(String[] args) {
        fillArray();
        sortArray(0);
        printArray();
    }
    // fill array with random values from 1 to 9
    public static void fillArray() {
        for (int ROW = 0; ROW < 5; ROW++) {
            System.out.println("");
            for (int COL = 0; COL < 2; COL++) {
                sort[ROW][COL] = (int) (Math.random() * 9) + 1;
            }
        }
    }
    // sort the array, int col will be the column to be sorted
    public static void sortArray(int col) {
        for (int ROW = 0; ROW < 5; ROW++) {
            list[ROW] = sort[ROW][col];
        }
        Arrays.sort(list);
        for (int i = 0; i < list.length; i++) {
            sort[i][col] = list[i];
        }
    }
    // print out the array in a visually appealing way
    public static void printArray() {
        for (int ROW = 0; ROW < 5; ROW++) {
            System.out.println("");
            for (int COL = 0; COL < 2; COL++) {
                System.out.print(sort[ROW][COL]);
            }
        }
    }
    

    }

    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

    #python 2.7
    def mysort(x, value):
        x.sort(key=lambda tup: tup[value])
        print x
    

    reply permalink

Content curated by @MaxBurstein