-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorting like Xcode. Fixes #615, with exceptions.
Showing
4 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Xcodeproj | ||
# Wrapper for a string that sorts by name like Xcode does. | ||
# @example | ||
# arrayOfFilenames.sort_by { |s| XcodeSortString.new(s) } | ||
class XcodeSortString | ||
include Comparable | ||
attr_reader :str, :scrubbed, :ints_and_strings, :i_s_pattern | ||
|
||
def initialize(str) | ||
@str = str | ||
@scrubbed = str.downcase | ||
# `Integer`+`rescue`: credit to https://stackoverflow.com/a/39025521 | ||
@ints_and_strings = @scrubbed.scan(/\d+|\D+/).map { |s| begin Integer(s, 10); rescue ArgumentError; s end } | ||
# comparing patterns: credit to https://rosettacode.org/wiki/Natural_sorting#Ruby | ||
@i_s_pattern = @ints_and_strings.map { |el| el.is_a?(Integer) ? :i : :s }.join | ||
end | ||
|
||
def <=>(other) | ||
if i_s_pattern.start_with?(other.i_s_pattern) || other.i_s_pattern.start_with?(i_s_pattern) | ||
compare = ints_and_strings <=> other.ints_and_strings | ||
if compare != 0 | ||
# we sort naturally | ||
compare | ||
else | ||
# equality, we sort reverse raw | ||
-(str <=> other.str) | ||
end | ||
else | ||
# type mismatch, we sort alphabetically | ||
scrubbed <=> other.scrubbed | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require File.expand_path('../../../../spec_helper', __FILE__) | ||
|
||
module ProjectSpecs | ||
describe Xcodeproj::XcodeSortString do | ||
before do | ||
@helper = XcodeSortString | ||
end | ||
|
||
#-------------------------------------------------------------------------# | ||
|
||
describe 'In general' do | ||
it 'sorts like Xcode' do | ||
unsorted_names = [ | ||
# basic values | ||
'a', 'a1', '1a', | ||
# spaces comparison (test disabled: not properly supported) | ||
#' a', 'a ', | ||
# case comparison | ||
'A', | ||
# pure integers | ||
'1', '2', '10', '01', | ||
# multi integers | ||
'0.1.1', '0.1.2', '0.1.10', '0.1.01', | ||
# equal integers | ||
'A1B001', 'A01B1', | ||
] | ||
sorted = unsorted_names.sort_by { |s| @helper.new(s) } | ||
# order given by Xcode 10.2 "Sort by Name" on macOS 10.14.4, English as primary language | ||
sorted.should == [ | ||
#' a', | ||
'0.1.1', | ||
'0.1.01', | ||
'0.1.2', | ||
'0.1.10', | ||
'1', | ||
'01', | ||
'1a', | ||
'2', | ||
'10', | ||
'a', | ||
'A', | ||
#'a ', | ||
'a1', | ||
'A1B001', | ||
'A01B1', | ||
] | ||
end | ||
end | ||
|
||
#-------------------------------------------------------------------------# | ||
end | ||
end |