Skip to content

Instantly share code, notes, and snippets.

@jyeary
Created May 1, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyeary/9615a3d1d1ace5f9726e to your computer and use it in GitHub Desktop.
Save jyeary/9615a3d1d1ace5f9726e to your computer and use it in GitHub Desktop.
An example of how to sort data elements by groupings, and determining where the starting line number. This assumes that the data is presented in the grouping shown {1, 0, 2, 6, 3, 5, 4}, and assumes you want to sort it in the order {0, 1, 2, 3, 4, 5, 6}
/*
* Copyright 2015 John Yeary <jyeary@bluelotussoftware.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bluelotussoftware.example;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author John Yeary <jyeary@bluelotussoftware.com>
* @version 1.0
*/
public class GroupedRowSorting {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] rowSortOrder = new int[]{1, 0, 2, 6, 3, 5, 4};
System.out.println(MessageFormat.format("Number of Groups: {0} and the sorting order {1}", rowSortOrder.length, Arrays.toString(rowSortOrder)));
Map<Integer, Integer> lengths = new TreeMap<>();
// Add some predictable bogus length values for each group of data.
for (int i = 0; i < rowSortOrder.length; i++) {
lengths.put(rowSortOrder[i], i + 1);// Here is where we would use a real length value.
}
// Print out our data so we can manually tabulate it to check results
System.out.println(MessageFormat.format("The array of length values per row {0}", lengths.entrySet()));
for (int i = 0; i < rowSortOrder.length; i++) {
int startingLineNumber = 0;
for (int j = 0; j < rowSortOrder[i]; j++) {
startingLineNumber += lengths.get(j);
}
System.out.println(MessageFormat.format("RowSortOrder {0} starts at line {1}", rowSortOrder[i], startingLineNumber));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment