/* * [SortTable.java] * * Summary: sort an html table by a column. * * Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1998-01-01 */ package com.mindprod.reorg; import com.mindprod.comparators.HTMLComparator; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import static java.lang.System.*; /** * sort an html table by a column. *

* Crude HTML table sorter. * arg 0-based column to sort, only sorts on one column. * to sort multiple columns, work least to most significant. * automatically picks numeric sort if all data numeric * Must manually strip stuff ahead and after table. * Must manually strip
* Must manually strip * extracts all in all tables and sorts together. discards rest. * no fancy etc. Just in upper or lower case. * It can't handle any grammatical errors in your HTML. * Must be terminal \n on last line of file * Input from E:\temp\in.html * Output to E:\temp\out.html all tags in lower case. *

* futures: sort multiple columns. * extract sort key from special columns. * deal with tags on * sort by String length. * extract sort info from column ignore html. * * @author Roedy Green, Canadian Mind Products * @version 1.0 1998-01-01 * @since 1998-01-01 */ public final class SortTable extends Common { private static final int FIRST_COPYRIGHT_YEAR = 1996; /** * non-displaying copyright */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; // end getMatrix /** * main * * @param args arg[0] 0-based col to sort on. only one allowed. * * @throws java.io.IOException if any problem reading or writing the html */ public static void main( String[] args ) throws IOException { /** * Which 0-based column we are sorting on */ final int colToSort; if ( args.length == 0 ) { colToSort = 0; } else { colToSort = Integer.parseInt( args[ 0 ] ); } // get entire table into a string. final String[][] matrix = getMatrix( IN_FILENAME ); final int rows = rows( matrix ); final int cols = cols( matrix ); displayRowAndColCounts( "input", rows, cols ); trimMatrix( matrix ); // Choosing Alpha or Numeric sort... boolean numericSort = isColNumeric( matrix, colToSort ); out.println( "Sorting " + ( numericSort ? "numerically" : "alphabetically" ) + " on column " + colToSort + " ..." ); Comparator comparator; if ( numericSort ) { comparator = new NumMatrixComparator( colToSort ); } else { comparator = new AlphaMatrixComparator( colToSort ); } Arrays.sort( matrix, comparator ); writeMatrix( matrix, OUT_FILENAME ); } // end main } /** * Sort a Matrix of Strings on a given column. *

* Defines an alternate sort order for String[]. */ class AlphaMatrixComparator implements Comparator { /** * which 0-based col to sort on */ private final int colToSort; public AlphaMatrixComparator( int colToSort ) { this.colToSort = colToSort; } /** * Sort a Matrix of Strings on a given column. * Defines an alternate sort order for String[]. * Compare two String[] Objects. * Compares . * Informally, returns (a-b), or +ve if a is more positive than b. * * @param a first String[] to compare * @param b second String[] to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( String[] a, String[] b ) { String aa = HTMLComparator.flatten( a[ colToSort ] ); String bb = HTMLComparator.flatten( b[ colToSort ] ); return aa.compareToIgnoreCase( bb ); } } /** * Sort a Matrix of Strings on a given numeric column. *

* Defines an alternate sort order for String[]. */ class NumMatrixComparator implements Comparator { /** * which 0-based col to sort on */ private final int colToSort; public NumMatrixComparator( int colToSort ) { this.colToSort = colToSort; } /** * Sort a Matrix of Strings on a given numeric column. * Defines an alternate sort order for String[]. * Compare two String[] Objects. * Compares . * Informally, returns (a-b), or +ve if a is more positive than b. * * @param a first String[] to compare * @param b second String[] to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( String[] a, String[] b ) { // very inefficient, converts on every compare!!!! // however tables are small, so the problem will not be too bad. String aa = HTMLComparator.flatten( a[ colToSort ] ); String bb = HTMLComparator.flatten( b[ colToSort ] ); double aaa = Double.parseDouble( aa ); double bbb = Double.parseDouble( bb ); if ( aaa > bbb ) { return 1; } if ( aaa < bbb ) { return -1; } return 0; } }