How is $search_results in search-results.tpl.php generated Where is $search_results generated?

I've got an Apache Solr search, and I want to reorganize the content in a certain way, and modifying the theming as well based on certain conditions, but all I have with $search_results is a string with a list.

What I want is to segregate search results based on a value in the object that Apache Solr creates.
About theming results, you should have 2 templates: search-results.tpl.php and search-result.tpl.php (without s).

The first one allows you to theme the list, the second one (without the s) allows you to theme the results individually.

So I would advise you to use a hook to sort/filter the results the way you want (see answers from Clive and arrubiu), but use the templates to define the design you want.