logo
down
shadow

Define custom sort order


Define custom sort order

By : Alessio Ortu
Date : November 20 2020, 03:01 PM
seems to work fine For the sake of completeness, here is the method that works, (adapting from VBA Type Mismatch on CustomOrder, link provided by SJR):
Define a Function that builds your sort list:
code :
Function Sortlist(TmpName) As String
    '(TmpName =  name of file with your sort values)

    Dim unicorns, i As Long
    Dim TmpBook as Workbook
    Set TmpBook = Workbooks.Open(TmpName)
    unicorns = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To unicorns
        Sortlist = Sortlist & "," & Range("A" & i)
    Next i
    Sortlist = Right(Sortlist, Len(Sortlist) - 1)  'trim the leading ","
    TmpBook.Close

End Function
 With ActiveSheet.Sort
    .SortFields.Clear
    .SetRange Range("A1:M" & LR)
    .SortFields.Add Key:=Columns("A")
    .SortFields.Add Key:=Columns("B")
    .SortFields.Add Key:=Columns("L"), CustomOrder:=YNSort
    .SortFields.Add Key:=Columns("H"), CustomOrder:=Sortlist(filename)
    .Header = xlYes
    .Apply
End With


Share : facebook icon twitter icon
collections.sort to define the sort order on two elements?

collections.sort to define the sort order on two elements?


By : Enrique Galván
Date : March 29 2020, 07:55 AM
should help you out I am writing my own comparator class called PercentComparator and called the sort as follows , If I got it right:
code :
class EngineComparator implements Comparator<Engine> {
    @Override
    public int compare(Engine o1, Engine o2) {
        int result = o1.getProdType().compareTo(o2.getProdType());
        return (result == 0) ? o1.getPercent().compareTo(o2.getPercent()) : result;
    }
}

Prod-Type   Percent
=======================
  A         1
  A         2
  A         3
  B         1
  B         2
  B         3
  C         1
  C         2
  C         3
How to define a product's display order in a smart collection with sort-order=manual, via API?

How to define a product's display order in a smart collection with sort-order=manual, via API?


By : Oliver
Date : March 29 2020, 07:55 AM
will help you A collection’s entries are called collects (I know, it’s weird), and that’s where Shopify stores the entry’s position in the collection.
Check out the "position" field on the Collect resource
Class define custom sort order

Class define custom sort order


By : Dragon
Date : March 29 2020, 07:55 AM
this one helps. An attribute is not a very good match for this, because you run into several problems:
You can't specify how each individual property should be compared, if there is a choice, because attributes need to work at compile time and don't allow complicated objects like an IComparer to be passed. You have to rely on the default. For strings, you could work around this by using StringComparison, which is an enum and can be passed to an attribute. Processing attributes means using reflection and generic code. This is fairly easy if you don't care a lot about performance and quite hard if you do. It also introduces more "magic" that may make the code harder to understand for maintainers. Because your class will not implement IComparable, no code that relies on IComparable for ordering can use your custom ordering out of the box. You could, in theory, implement IComparable through a base class, but this doesn't work if you need to integrate with an existing object hierarchy. You're stuck with one particular way to order the items. This is OK if you consider this the "default" order and have some way of doing a custom comparison if necessary.
code :
[AttributeUsage(AttributeTargets.Property)]
class SortKeyAttribute : Attribute { }
class SortSample {
    [SortKey]
    public DateTime Date { get; set; }
    public string Name { get; set; }
}
IEnumerable<SortSample> outputList = list.OrderByKey();
public static class EnumerableExtensions {
    public static IOrderedEnumerable<T> OrderByKey<T>(this IEnumerable<T> sequence) {
        return sequence.OrderBy(x => x, getKeyComparer<T>());
    }

    static IComparer<T> createComparer<T, TKey>(PropertyInfo key) {
        Func<T, TKey> keySelector = x => (TKey) key.GetValue(x);
        var keyComparer = Comparer<TKey>.Default;
        return Comparer<T>.Create(
            (x, y) => keyComparer.Compare(keySelector(x), keySelector(y))
        );
    }

    static IComparer<T> getKeyComparer<T>() {
        List<PropertyInfo> sortKeyProperties = (
            from p in typeof(T).GetProperties()
            let a = (SortKeyAttribute) p
                .GetCustomAttributes(typeof(SortKeyAttribute))
                .FirstOrDefault()
            where a != null
            select p
        ).ToList();
        if (sortKeyProperties.Count == 0) {
            // with no [SortKey], fall back to the default comparer
            return Comparer<T>.Default;
        }
        if (sortKeyProperties.Count > 1) {
            throw new InvalidOperationException(
                $"Multiple sort keys specified for class '{typeof(T).FullName}'."
            );
        }
        PropertyInfo sortKeyProperty = sortKeyProperties[0];
        MethodInfo createComparerMethodInfo = typeof(EnumerableExtensions)
            .GetMethod("createComparer", BindingFlags.NonPublic | BindingFlags.Static)
        ;
        return (IComparer<T>) createComparerMethodInfo
            .MakeGenericMethod(typeof(T), sortKeyProperty.PropertyType)
            .Invoke(null, new[] { sortKeyProperty })
        ;
    }
}
How to define a custom compare function to sort a matrix according as one dimensional array sort

How to define a custom compare function to sort a matrix according as one dimensional array sort


By : Julio Cesar
Date : March 29 2020, 07:55 AM
will help you Moving the rows of the matrix around during sorting is something you should avoid. Instead I would sort a vector of
code :
struct IdAndIndex{ 
    double id;
    int index;
};
struct IdAndRow {
    double id;
    double[3] row;
    bool operator<(const IdAndRow& other) { return id < other.id; }
};
How do I use Comparator to define a custom sort order?

How do I use Comparator to define a custom sort order?


By : Shahin Farhood
Date : March 29 2020, 07:55 AM
seems to work fine I recommend you create an enum for your car colours instead of using Strings and the natural ordering of the enum will be the order in which you declare the constants.
shadow
Privacy Policy - Terms - Contact Us © voile276.org