Sort File List in Xcode?
Asked Answered
S

5

37

Is there a way in Xcode to sort my list of files under say the Classes folder Alphabetically?

I know I can drag them around, but with tons of files that is a pain.

I am surprised I can not right click on the folder and say to sort.

Syphilology answered 27/10, 2008 at 5:20 Comment(0)
E
41

Click on the folder, and then click Edit > Sort > By Name

Earthenware answered 27/10, 2008 at 6:5 Comment(7)
Seems that option is gone in XCode 4. Any idea how to do it in the new version?Askins
I have added a follow up question for XCode4 as this has not received. https://mcmap.net/q/297150/-sort-file-list-in-xcode4Kite
Not sure about earlier versions of Xcode but in 4.5 it's under Edit > Sort > By Name. Or, Control click then choose "Sort By Name"Manaker
@SnowCrash My Edit / Sort / By Name is gray when I have Build Phases selected.Status
@SnowCrash, do I have to do it manually every time I add new files? I am using XCode 5 but it does not seem to automatically rearrange the sort order when I add new files.Stale
absolute one shot downCrandale
2015, I still don't get why I need to click something to get my file sorted!Hawes
P
4

Here is a Ruby script that will sort all the files within their respective groups in an Xcode 4 project file (probably Xcode 3 as well but I haven't tried that).

Usage:

ruby sort.rb <infile> <outfile>

where <infile> is an unsorted .pbxproj file and <output> will be the sorted version. Don't make them the same file.

#!/usr/bin/env ruby

state = :primary
group = []
file_count = group_count = 0

File.open ARGV[0] do |infile|
  File.open ARGV[1], 'w' do |outfile|
    infile.each_line do |line|
      case state

      when :primary
        # copy lines until and including "children = ("
        outfile.write line
        state = :group if line =~ /^\s*children\s*=\s*\x28\s*$/

      when :group
        if line =~ /^\s*[0-9A-F]+\s*\/\* (.*) \*\/,\s*$/
          # add file to current group if "<guid> /* <filename> */,"
          group << [$1,line]
          file_count += 1

        else
          # otherwise, output sorted files,
          # empty the group, and go back to primary state
          group.sort.each do |fn,ln|
            outfile.write ln
          end

          state = :primary
          group = []
          outfile.write line
          group_count += 1
        end

      end
    end
  end
end

puts "Sorted #{file_count} files in #{group_count} groups"
Postmillennialism answered 3/4, 2011 at 12:33 Comment(0)
F
0

The ruby script from jedediah works great. To also sort resources being copied, you can add:

state = :group if line =~ /^\s*files\s*=\s*\x28\s*$/

Note that sort is case sensitive (capital letters first). To make it insensitive, use:

group << [$1.downcase,line]
Fayum answered 1/3, 2012 at 14:24 Comment(0)
S
0

There isn't really an easy solution in XCode5.

  • I opened the pbxproj file in a text editor.
  • Navigate down to /* Begin PBXResourcesBuildPhase section */
  • select everything in files.
  • copy to a new text document.
  • Replace /* with \t (tab character)
  • select all, copy and paste into blank excel document. you should have 2 columns of data
  • insert a column at poisition 2
  • make all rows for that column /*
  • sort the sheet on column 3
  • copy all data and paste back over your section in pbxproj file
  • save file

That should sort the "Copy Bundle Resources" section of your project.

I feel dirty just doing this, but hey - it works

Senator answered 5/5, 2014 at 8:57 Comment(0)
T
-1

Czar there are advantages to having it the way you want, instead of automatically having it sort at all times.

Some classes might be related in some way, but the names aren't right next to each other, I've used that for certain. :)

Treacle answered 24/11, 2010 at 12:32 Comment(3)
-1 because "You don't always have to sort your files" is not an answer to "How do I sort my files?"Milestone
I couldn't comment since I didn't have any rep. So it came in as an answer.Treacle
Personally, I don't think people should be modded down if they are trying to help.Whore

© 2022 - 2024 — McMap. All rights reserved.