Ruby Python Java Case insensitive sorting

Introduction

Speaking of sorting, it is in numerical order and dictionary order, but I investigated other sorts.

Fitted story

Read @ jag_507's * Ruby learning with AtCoder 10 [1st algorithm practice test DoubleCamelCase Sort] * and * AtCoder 1st algorithm practice I tried F-DoubleCamelCase Sort *, but the sorting doesn't work.

sort.rb


a = ["FisH", "DoG", "CaT", "AA", "AaA", "AbC", "AC"]
a.sort
 # => ["AA", "AC", "AaA", "AbC", "CaT", "DoG", "FisH"] #Actual return value
      ["AA", "AaA", "AbC", "AC", "CaT", "DoG", "FisH"] #Expected return value

This article: * [sort command, basics and applications and traps](https://qiita.com/richmikan@github/items/cc4494359b1ac2f72311#-f%E8%BE%9E%E6%9B%B8%E9%A0% 86% E3% 81% AB% E4% B8% A6% E3% 81% B9% E3% 82% 8B) * Is there a -f option like ~~ Doraemon ~~ Help Google teacher.

Case insensitive sorting

Many programming languages, not just * Ruby *, sort by dictionary order by ASCII code order, so uppercase and lowercase letters are younger. Therefore, we need a case-insensitive sort. Ruby

ruby.rb


a = ['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C', 'D', 'E']
p a.sort
 # => ["A", "B", "C", "D", "E", "a", "b", "c", "d", "e"]
p a
 # => ["a", "b", "c", "d", "e", "A", "B", "C", "D", "E"]
p a.sort{|x, y| x.casecmp(y).nonzero? || x <=> y}
 # => ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e"]
p a.sort_by{ |s| [s.downcase, s] }
 # => ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e"]

python.py


a = ['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C', 'D', 'E']
print(sorted(a))
 # => ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
print(a)
 # => ['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C', 'D', 'E']
print(sorted(sorted(a), key=str.lower))
 # => ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e']

java.java


        List<String> a = Arrays.asList("a", "b", "c", "d", "e", "A", "B", "C", "D", "E");
        a.sort(Comparator.naturalOrder());
        System.out.println(a); // [A, B, C, D, E, a, b, c, d, e]
        a.sort(String.CASE_INSENSITIVE_ORDER);
        System.out.println(a); // [A, a, B, b, C, c, D, d, E, e]

For *** Java ***, CASE_INSENSITIVE_ORDER is prepared.

Summary

Referenced site

Recommended Posts

Ruby Python Java Case insensitive sorting
Java VS PHP VS Python VS Ruby
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
[Ruby / Python / Java / Swift / JS] What is an algorithm?
Ruby, Python and map
Python and Ruby split
[Python] Sorting Numpy data
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
AtCoder ABC168 A case expression solved in Ruby and Python
Python on Ruby and angry Ruby on Python
Python, Java, C ++ speed comparison
Standard input / summary / python, ruby
I compared Java and Python!
Sorting image files with Python (2)
Sorting image files with Python (3)
Techniques for sorting in Python
Zundokokiyoshi with python / ruby / Lua
About Perl, Python, PHP, Ruby
Sorting image files with Python
Ruby and Python syntax ~ branch ~
Ruby, Python Module Installation Guide
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial