github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/sort/subsort/doc.go (about)

     1  // Package subsort is for sorting slices of any struct,
     2  // to be sorted by some field *in* the struct.
     3  //
     4  // We can standardize / generify this sorting,
     5  // provided we stick to a few narrowing assumptions
     6  //
     7  // 1.) We want to sort slices of any struct
     8  // 2.) We want to sort by a *string* or *int* field
     9  // 3.) Float values could be string formatted %04.4d, with a huge base summand, avoiding negative values
    10  // 4.) Because we need our slices in *several* sort orders at the same time,
    11  //		we always want to *copy* the slice
    12  // 5.) To minimize memory footprint, we only copy the sort field
    13  //      and an int reference to the original array.
    14  //      Thus we can retrieve other values from the original slice
    15  // 6.) It's expensive to convert slices of various types to
    16  //		slices of interfaces to slices of SortedByVal, that is:
    17  //			[]TypeX to []interface{} to []SortedByVal
    18  //		Therefore we resort to creating desired []SortedByVal outside.
    19  // 7.) Similar mechanisms for float, time could be added
    20  // 8.) Maintenance by file diffing
    21  //
    22  // We don't need to return pointers to the sorted slices,
    23  // since slices are already references to the underlying array.
    24  //
    25  // SortedBy[String/Int]Val sadly needs to be exported,
    26  // because the sorted slices will be given to the outside callee
    27  // with type []SortedBy...Val
    28  //
    29  // I tried to cascade the sort interface implementations,
    30  // using a "base" type for .Len() and have the others wrap it,
    31  // but strangely, compiler demands implementation at the top level.
    32  // Thus we end up with *four* identical implementations of Len(),Swap()
    33  // and four *almost identical* implementations of Less()
    34  //
    35  //
    36  package subsort