Sublime Forum

Sort search results by last modified

#1

Hello,

When I use the find feature to search for text in all files within my project I might get a long list of matching files. How are these matching files sorted and is it possible to change the order in which these files are presented?
I would like for example to have the results sorted by “last modified” date.

Thanks

1 Like

#2

There’s no way to sort the results currently, but you’re welcome to make a feature request on the public issue tracker: https://github.com/sublimehq/sublime_text/issues/new/choose

1 Like

#3

Do you know how are results sorted now? Thanks for your reply.

0 Likes

#4

They’re not sorted. Whatever the filesystem returns and whatever file gets searched through first is what turns up first in the results.

0 Likes

#5

So basically the order is random? I have a hard time believing that there’s no criteria on how the filesystem returns files.

0 Likes

#6

The search results aren’t in the order that the filesystem gives back. Files are searched in parallel and thus results are mostly random.

0 Likes

#7

here is a plugin, so by pressing a keyboard shortcut the results are sorted:

The plugin:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime, sublime_plugin, unicodedata
import os, re
import datetime as dt

class sort_searchresults_github_thread(sublime_plugin.TextCommand):
	def run(self, edit):
		print('hello')
		point = self.view.sel()[0].b
		body = self.view.substr(sublime.Region(0, self.view.size()))
		file_results_listofdicts = []
		results = body.split('\n\n')
		file_pattern = r'.*' # here you can specify patterns for the path line, e. g. '\.txt'
		filenumber_pattern = r'Searching \d{1,9} files for .*'
		matchnumber_pattern = r'\d{1,9} matches across \d{1,9} files'
		end_str = ''
		for result in results:
			if re.search(filenumber_pattern, result):
				match = re.search(filenumber_pattern, result)
				result = match.group()
				print("filenumber found:", result)
				end_str = end_str + result
			elif re.search(matchnumber_pattern, result):
				match = re.search(matchnumber_pattern, result)
				result = match.group()
				print("matchnumber found:", result)
				end_str = f'{end_str} | {result}'
			elif re.search(file_pattern, result):
				matches = re.findall(file_pattern, result)
				path = matches[0]
				context = ''
				for match in matches[1:]:
					if not match == '':
						context += f'{match}\n'
				file_results_listofdicts.append({'path': path, 'context': context.rstrip()})
		sorted_listofdicts = sorted(file_results_listofdicts, key=lambda x: x['path'])
		region = sublime.Region(0, self.view.size())
		self.view.erase(edit, region)
		for result_dict in sorted_listofdicts:
			self.view.insert(edit, 0, f"{result_dict['path']}\n{result_dict['context']}\n\n") # neueste Ergenbisse oben
			#self.view.insert(edit, self.view.size(), f"{result_dict['result']}\n\n") # neueste Ergenbisse unten
		self.view.insert(edit, 0, f"{end_str}\n\n")


on a mac save the plugin in:

/Users/your_username/Library/Application Support/Sublime Text/Packages/User/plugin_sort_searchresults_github_thread.py

key binding:

{ "keys": ["alt+s"], "command": "sort_searchresults_github_thread" },

If you hit ‘cmd+f’ after the sorting, it even highlights your searchterm, so you get the same result as in sublimetext 3.

0 Likes