You can just use a Map to do this, perhaps like this:
Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
ContentDocument[] docs = docsByAuthor.get(doc.CreatedById);
if(docs == null) {
docsByAuthor.put(doc.CreatedById, docs = new ContentDocument[0]);
}
docs.add(doc);
}
The result of this code would be a Map where the Key is the CreatedById for each group of documents created by the same person, and the value a list of their documents. You'll see some variants of this code out on the Internet, such as:
Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
if(!docs.containsKey(doc.CreatedById)) {
docsByAuthor.put(doc.CreatedById, new ContentDocument[0]);
}
docsByAuthor.get(doc.CreatedById).add(doc);
}
Or:
Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
if(docs.containsKey(doc.CreatedById)) {
docsByAuthor.get(doc.CreatedById).add(doc);
} else {
docsByAuthor.put(doc.CreatedById, new List<ContentDocument>{ doc });
}
}
etc.
I prefer my version because it appears to have the best benchmark of all variants I've found and tested, but do feel free to use whichever you feel most comfortable with and understand, as the performance difference is usually pretty small.
Map<String, List<Document>>
(the key of the map being the name of the author).