web stats
Sort/Rearrange OBX segments based on OBX-3 code/name? - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Thread Tools Display Modes
Old 06-13-2013, 05:13 AM
jasonhuang jasonhuang is offline
Mirth Newb
Join Date: Oct 2012
Posts: 18
jasonhuang is on a distinguished road
Default Sort/Rearrange OBX segments based on OBX-3 code/name?


We are sending result messages to a receiving system that requires OBX segments to be sorted based on orders of the OBX-3 content. Unfortunately our system doesn't generates OBXs in the same order required by the receiving site.

Has anyone done similar task before? - Sorting OBX segments based on the code or name in OBX-3. For example, if I have three OBXs below:


And I want to sort them based on their code in OBX-3.1 so the output will be:
(only consider the value of code before the "-")

How should I proceed? Any comments/suggestions will be greatly appreciated!

Reply With Quote
Old 06-13-2013, 05:57 AM
cory_cole cory_cole is offline
Mirth Guru
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road

var tmpOBX = new Array();
for each (obx in msg..OBX)//sort OBX
if (tmpOBX.length == 0)
for(i = 0; i < tmpOBX.length; i++)
if (tmpOBX[i]['OBX.3']['OBX.3.1'].toString() > obx['OBX.3']['OBX.3.1'].toString())
tmpOBX.insert(i, obx);
i = tmpOBX.length; //short circuit if inserted
if(i == tmpOBX.length - 1)//if last at end of list add to end

for(j = 0; j < tmpOBX.length; j++)//renumber OBXs and replace in msg
tmpOBX[j]['OBX.1']['OBX.1.1'] = j + 1;
msg['OBX'][j] = tmpOBX[j];
Reply With Quote
Old 06-13-2013, 07:02 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road

You could use Collections as well:

var obxList = new java.util.ArrayList();
for each (obx in msg.OBX)

java.util.Collections.sort(obxList,new java.util.Comparator({compare:function(o1, o2) {
	function getIDValue(node) {
		var obsId = node['OBX.3']['OBX.3.1'].toString();
		return parseInt(obsId.substr(0,obsId.indexOf('-')),10);
	return getIDValue(o1) - getIDValue(o2);

for (var i = 0; i <= obxList.size()-1; i++) {
	msg.OBX[i] = obxList.get(i);
	msg.OBX[i]['OBX.1']['OBX.1.1'] = i+1;
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.

- How do I foo?
- You just bar.
Reply With Quote
Old 06-13-2013, 08:02 AM
jasonhuang jasonhuang is offline
Mirth Newb
Join Date: Oct 2012
Posts: 18
jasonhuang is on a distinguished road


thanks Cory and narupley!
Reply With Quote
Old 10-08-2018, 04:14 PM
larryseieroe larryseieroe is offline
What's HL7?
Join Date: Oct 2018
Location: Omaha
Posts: 3
larryseieroe is on a distinguished road

I have a similar issue.

i need to sort these statements
i have copied the numbers from OBX.3.1 to OBX.4.1 and would like to order numerically on OBR.4.1

any help would be much appreciated, in my testing it looks like Java script is sorting by string values, not numerical.

OBX|10||Statement 20|20|Date: mmddyyyy||||||AS
OBX|11||Statement 21|21|Time: hh:mm:ss||||||AS
OBX|12||Statement 22|22|User: N/A||||||AS
OBX|13||Statement 23|23|Computer: <ComputerName>||||||AS
OBX|14||Statement 24|24|Description:||||||AS
OBX|15||Statement 25|25|The Windows Installer service entered the stopped state.||||||AS
OBX|16||Statement 26|26|For more information, see Help and Support Center at <http://go.microsoft.com/fwlink/events.asp>.||||||AS
OBX|17||Statement 27|27|||||||AS
OBX|18||Statement 28|28|larry||||||AS
OBX|19||Statement 29|29|Seieroe||||||AS
OBX|20||Statement 30|30|||||||AS
OBX|21||Statement 31|31|was here today||||||AS
OBX|22||Statement 1|1|||||||AS
OBX|23||Statement 2.1|2.1|The system Event log will show that the Windows Installer Service is starting and stopping||||||AS
OBX|24||Statement 2.2|2.2|automatically.||||||AS
OBX|25||Statement 3|3|||||||AS
OBX|26||Statement 4|4|Event Type: Information||||||AS
OBX|27||Statement 5|5|Event Source: Service Control Manager||||||AS
OBX|28||Statement 6|6|Event Category: None||||||AS
OBX|29||Statement 7|7|Event ID: 7035||||||AS
OBX|30||Statement 8|8|Date: mmddyyyy||||||AS
Larry aka(Skippy)
Reply With Quote

hl7, obx, sort

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -8. The time now is 01:20 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation