Hi
I have an Index file in which is not sorted alphabetically. Each row within an Index is a paragraph. I want to sort it alphabetically and update it in the same document. I have gone through some suggestion mention in different topics but doesn’t seem to be working.
E.G Input:
Active surveillance XXX
Absolute risk XXX
Adjustment XXX
indirect XXX
direct XXX
expected output:
Absolute risk XXX
Active surveillance XXX
Adjustment XXX
direct XXX
indirect XXX
@ELSSAM_elsevier_com
Unfortunately, Aspose.Words does not provide API to sort paragraphs by text. Could you please ZIP and attach your input and expected output Word documents? We will then log this missing feature in our issue tracking system according to your requirement.
I couldn’t find any implementation, so I did mine which is working as expected. Here is the code for it:
private void ProcessParagraphSorting(Document doc) {
const string sortAsc = "SortParagraphAsc";
const string sortDesc = "SortParagraphDesc";
var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)
.Select(x => x as StructuredDocumentTag)
.Where(x => GetSdtTag(x) == sortAsc || GetSdtTag(x) == sortDesc);
foreach (var sdt in sdts)
{
var paras = sdt.GetChildNodes(NodeType.Paragraph, true)
.Select(x => x as Aspose.Words.Paragraph);
var clonedParas = paras.Select(p => (Aspose.Words.Paragraph)p.Clone(true)).ToList();
// Sort text by text
if (GetSdtTag(sdt) == sortAsc)
{
clonedParas.Sort((a, b) => string.Compare(a.Range.Text, b.Range.Text, StringComparison.OrdinalIgnoreCase));
}
else
{
clonedParas.Sort((a, b) => string.Compare(b.Range.Text, a.Range.Text, StringComparison.OrdinalIgnoreCase));
}
// Remove all current paragraphs from the SDT
foreach (var para in paras)
{
para.Remove();
}
// Add cloned paragraphs in the desired sorting order
foreach (var para in clonedParas)
{
sdt.AppendChild(para);
}
}
}