Sorting/Ordering entities by metadata


I have made a plugin in which I m listing all files entities of a user in a table and that table will sort and search based on the entity title and its metadata's , but there is no option in elgg to sort metadata, my current code is :

$entities = elgg_get_entities(array('type' => 'object', 'subtype' => 'file', 'owner_guid' => $owner_guid, 'limi' => $limit, 'offset' => $offset));

foreach($entities as $entity)


$simpletype = elgg_get_metadata_byname('simpletype', $entity->guid);

$filename = elgg_get_metadata_byname('filename', $entity->guid);


this is getting correctly in the table , but there is function in elgg to order the entities using meta data, so basically what I want to do to that I be able to sort these entities based on all these values , title , simpletype and filename  . Is there a function in elgg now ? If not can someone devise me something ?