We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to do a field-sorting in word?

First of all, I’m so sorry about my english. I hope somebody could understand what I am talking about.

Here is my question : how can do a field-sorting in the word instead of DB?

for example:
I have a table named test_sort in database, all of its columns are :

id varchar(32) pk,
username varchar(32),
age int(4),
birthday timestamp,
company_name varchar(32).

I want to display all records of table test_sort in Microsoft Word and sort them order by increasing.
We could use sql script:

select username, 
from test_sort 
order by username, 
company_name asc

But some other day, boss need me to display these records order by username increasing, age increasing and birthday decreasing, company_name decreasing.
So I have to change these sql script like:

select username, 
from test_sort 
order by username asc,
age asc,
birthday desc, 
company_name desc

And another day, boss has new requirement, and I have to change these sql script agian and agian.
So I think whether I could do the sort in Microsoft Word indtead of database, just like add or edit a mail merge field?


Thanks for your inquiry. I think, in your case, you can use DataView as data source for mail merge:
In this case, you can sort your data as you need on the fly.
Hope this helps. Please let me know in case of any issues, I will be glad to help you.
Best regards,

alexey, thank you so much!
I am trying to generated a word like you told me, I will tell you the result some time later.
anain, thank you so much.

It seams the problem can not be solved with the link you gave to me.
Because it just make me to do the operation from sql script to java code.
I have to update java file instead of sql script.
I hope to update a .doc file just like :
username:asc,age:desc,birthday:asc and <<company_name:desc>> or something else like this.
And then I could tell my boss to do sort by himself.
He just need to update this .doc template and generate a new file.


Thanks for your inquiry. I think, you can pass “sorting column” as a parameter to the method, which generates your document. In this case, it will not be necessary to change your code or SQL query each time when you need to change “sorting field”.
Best regards.

Thank you for you feedback, alexey.

At first, I want to pass “sorting column” as a parameter to the query method, but at last, I give it up and try some other way.
Because I don’t know which column should be sort and what it’s order.
Boss may like sort column “username” and order by “asc”, and some time later he will not want to sort column “username” but “company_name” order by “desc”.
So I think let boss edit the text of MergeField is a good way.


Thank you for additional information. But from my point of view, it would be easier for users just select “sorting field” from dropdown and click “Ok”, than modify template each time when you need to change sorting field.
Best regards.

Thanks for using Aspose.Words.
If I understand you correctly, you want to be able to set a flag in a mergefield in a document and have the data that is merged in that field automatically sorted in what way the flag is set too i.e. ascending or descending. This would be useful for you so you don’t need to constantly recode a query to a database and so forth. This means instead you could just have the same document and then change the sort flag in the mergefield and run a premade program to sort in a different way, much like using the Sort function in Word.
If this is what you are trying to achieve, then yes this is certainly possible using Aspose.Words. There is no native API call to sort a table but implementing one using the other API methods is quite straight forward. If this is what you are trying to do then we can help you further, if I have misunderstood you, could you please further clarify what is it you are trying to do.