Case-Insensitive List Sorting, Without Lowercasing the Result

case-insensitive list sorting, without lowercasing the result?

In Python 3.3+ there is the str.casefold method that's specifically designed for caseless matching:

sorted_list = sorted(unsorted_list, key=str.casefold)

In Python 2 use lower():

sorted_list = sorted(unsorted_list, key=lambda s: s.lower())

It works for both normal and unicode strings, since they both have a lower method.

In Python 2 it works for a mix of normal and unicode strings, since values of the two types can be compared with each other. Python 3 doesn't work like that, though: you can't compare a byte string and a unicode string, so in Python 3 you should do the sane thing and only sort lists of one type of string.

>>> lst = ['Aden', u'abe1']
>>> sorted(lst)
['Aden', u'abe1']
>>> sorted(lst, key=lambda s: s.lower())
[u'abe1', 'Aden']

Sort list of strings ignoring upper/lower case

The sort() method and the sorted() function take a key argument:

var.sort(key=lambda v: v.upper())

The function named in key is called for each value and the return value is used when sorting, without affecting the actual values:

>>> var=['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=lambda v: v.upper())
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

To sort Ant before ant, you'd have to include a little more info in the key, so that otherwise equal values are sorted in a given order:

>>> sorted(var, key=lambda v: (v.upper(), v[0].islower()))
['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']

The more complex key generates ('ANT', False) for Ant, and ('ANT', True) for ant; True is sorted after False and so uppercased words are sorted before their lowercase equivalent.

See the Python sorting HOWTO for more information.

How to sort alphabetically while ignoring case sensitive?

Here's a plain java example of the best way to do it:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorter {
String fruits[] = new String[7];
List<String> lst;

Sorter() {
lst = new ArrayList<String>();
// initialise UNSORTED array
fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach";
fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple";
fruits[6] = "banana";
}

public static void main(String[] args) {
Sorter srt = new Sorter();
srt.anyOldUnstaticMethod();

}
public void anyOldUnstaticMethod() {
Collections.addAll(lst, fruits);
System.out.println("Initial List");
for (String s : lst)
System.out.println(s);
Collections.sort(lst);
System.out.println("\nSorted List");
for (String s : lst)
System.out.println(s);
Collections.sort(lst, new SortIgnoreCase());
System.out.println("\nSorted Ignoring Case List");
for (String s : lst)
System.out.println(s);
}

public class SortIgnoreCase implements Comparator<Object> {
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
}
}

python sort multi-dimensional lists CASE INSENSITIVE

Use key=lambda lst: lst[2].lower().

JavaScript case insensitive sorting for HTML table

This function works to sort strings alphabetically and places uppercase before lowercase.

function (a, b) {
var x = String(a).toLowerCase();
var y = String(b).toLowerCase();

if (x > y)
return -1;
if (x < y)
return 1;
}

How can I sorting case insensitive by realm?

You can only do case-sensitive queries by default.

If you want to sort case-insensitive, then you should store a second field that is full lowercase.

private String name;

@Index
private String nameLowerCase;

public void setName(String name) {
this.name = name;
if(name != null) {
nameLowerCase = name.toLowerCase();
} else {
nameLowerCase = null;
}
}

And

mRealm.where(Member.class).sort("nameLowerCase").findAll();


Related Topics



Leave a reply



Submit