How do I insert an element at the correct position into a sorted array in Swift?

Here is a possible implementation in Swift using binary search (from with slight modifications): extension Array { func insertionIndexOf(_ elem: Element, isOrderedBefore: (Element, Element) -> Bool) -> Int { var lo = 0 var hi = self.count – 1 while lo <= hi { let mid = (lo + hi)/2 if isOrderedBefore(self[mid], elem) { … Read more

Sorting an ArrayList of Objects alphabetically

The sorting part can be done by implementing a custom Comparator<Vehicle>. Collections.sort(vehiclearray, new Comparator<Vehicle>() { public int compare(Vehicle v1, Vehicle v2) { return v1.getEmail().compareTo(v2.getEmail()); } }); This anonymous class will be used for sorting the Vehicle objects in the ArrayList on the base of their corresponding emails alphabetically. Upgrading to Java8 will let you also … Read more

Alphanumeric sorting with PostgreSQL

The ideal way would be to normalize your design and split the two components of the column into two separate columns. One of type integer, one text. With the current table, you could: SELECT col FROM tbl ORDER BY (substring(col, ‘^[0-9]+’))::int — cast to integer , substring(col, ‘[^0-9_].*$’); — works as text The same substring() … Read more

Django ListView – Form to filter and sort

You don’t need post. Pass the filter value and order_by in the url for example: …/update/list/?filter=filter-val&orderby=order-val and get the filter and orderby in the get_queryset like: class MyView(ListView): model = Update template_name = “updates/update.html” paginate_by = 10 def get_queryset(self): filter_val = self.request.GET.get(‘filter’, ‘give-default-value’) order = self.request.GET.get(‘orderby’, ‘give-default-value’) new_context = Update.objects.filter( state=filter_val, ).order_by(order) return new_context def … Read more