# Sorted GROUP\_CONCAT

**URL:** <https://www.singlestore.com/forum/t/sorted-group-concat/1185>\
**Category:** Help\
**Created:** [August 30, 2019, 4:48pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185 "2019-08-30T16:48:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [August 30, 2019, 4:48pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/1 "2019-08-30T16:48:31Z")

</div>

Is it possible to sort the values in a GROUP\_CONCAT? There does not appear to be an ORDER BY clause and I tried nesting the sort but it did not seem to work. If not, are there any other ways to achieve this requirement? I found [this post](https://stackoverflow.com/questions/48507570/group-concat-with-order-in-memsql) on StackOverflow, which suggests using an analytical function such as row number and sorting by that.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [August 30, 2019, 5:19pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/2 "2019-08-30T17:19:04Z")

</div>

This syntax will work in the upcoming 7.0 release:

```
SELECT dept_id, 
GROUP_CONCAT (emp_id ORDER BY emp_id SEPARATOR', ') 
as "employees ids" 
from employee group by dept_id;

```

But ORDER BY in GROUP\_CONCAT is not supported in 6.8. A possible workaround would be to write a UDF, say SORT\_GROUP, to parse the result of GROUP\_CONCAT and sort it in an array, e.g.

```
SELECT dept_id, 
SORT_GROUP(GROUP_CONCAT (emp_id SEPARATOR', '))
as "employees ids" 
from employee group by dept_id;

```

The SPLIT function ([SingleStoreDB Cloud · SingleStore Documentation](https://docs.memsql.com/sql-reference/v6.8/split/)) would make it easier to write.

---

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [September 27, 2019, 12:37pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/3 "2019-09-27T12:37:51Z")

</div>

Is there a function to sort an array or does that need to be coded in this function too? I can’t find a documented list of array functions such as LENGTH, although it’s used in examples. (LENGTH is only documented as a string function.)

---

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [September 27, 2019, 1:17pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/4 "2019-09-27T13:17:11Z")

</div>

Also, I’m using 6.7, so the SPLIT function isn’t available either. ☹

---

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [September 27, 2019, 6:20pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/5 "2019-09-27T18:20:44Z")

</div>

I came up with this for now.

> DELIMITER //  
> CREATE OR REPLACE FUNCTION TEST(I\_STRING LONGTEXT, I\_OLD\_SEP LONGTEXT, I\_NEW\_SEP LONGTEXT) RETURNS LONGTEXT AS  
> DECLARE  
> V\_STRING LONGTEXT;  
> V\_NEXT\_SEP INT;  
> A\_SPLIT ARRAY(LONGTEXT);  
> V\_ELEM\_COUNT INT=-1;  
> V\_SWAP LONGTEXT;  
> V\_RESULT LONGTEXT;  
> BEGIN  
> V\_STRING=I\_STRING;  
> V\_NEXT\_SEP=INSTR(V\_STRING,I\_OLD\_SEP);
> 
> WHILE V\_NEXT\_SEP\>0 LOOP  
> A\_SPLIT+=CREATE\_ARRAY(1);  
> V\_ELEM\_COUNT=V\_ELEM\_COUNT+1;  
> A\_SPLIT[V\_ELEM\_COUNT]=LEFT(V\_STRING,V\_NEXT\_SEP-1);  
> V\_STRING=SUBSTR(V\_STRING,V\_NEXT\_SEP+1);  
> V\_NEXT\_SEP=INSTR(V\_STRING,I\_OLD\_SEP);  
> END LOOP;
> 
> IF V\_STRING IS NOT NULL THEN  
> V\_ELEM\_COUNT=V\_ELEM\_COUNT+1;  
> A\_SPLIT+=CREATE\_ARRAY(1);  
> A\_SPLIT[V\_ELEM\_COUNT]=V\_STRING;  
> END IF;
> 
> FOR i IN 0 … LENGTH(A\_SPLIT)-2 LOOP  
> IF A\_SPLIT[i]\>A\_SPLIT[i+1] THEN  
> V\_SWAP=A\_SPLIT[i];  
> A\_SPLIT[i]=A\_SPLIT[i+1];  
> A\_SPLIT[i+1]=V\_SWAP;  
> END IF;  
> END LOOP;
> 
> FOR i IN 0 … LENGTH(A\_SPLIT)-1 LOOP  
> IF i=0 THEN  
> V\_RESULT=A\_SPLIT[i];  
> ELSE  
> V\_RESULT=CONCAT(V\_RESULT,I\_NEW\_SEP,A\_SPLIT[i]);  
> END IF;  
> END LOOP;
> 
> RETURN V\_RESULT;  
> END //  
> DELIMITER ;

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [September 27, 2019, 10:40pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/6 "2019-09-27T22:40:30Z")

</div>

I made note of your requests to document LENGTH for arrays and to add a built-in ARRAY sort routine.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [September 27, 2019, 10:50pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/7 "2019-09-27T22:50:21Z")

</div>

Looks like your sort loop won’t always leave the array fully sorted. May need to add an outer loop or use another approach.

---

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [September 27, 2019, 11:19pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/8 "2019-09-27T23:19:44Z")

</div>

Thanks for pointing that out. I wrote that based on some sort pseudo-code I found, and it worked for my first test cases, but I see why that won’t always work.

---

<div class="post-metadata">

**Author:** ![bvincent](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eb8c5e/32.png) [@bvincent](https://www.singlestore.com/forum/u/bvincent)\
**Post date:** [September 28, 2019, 3:44pm UTC](https://www.singlestore.com/forum/t/sorted-group-concat/1185/9 "2019-09-28T15:44:28Z")

</div>

This seems to work.

> DELIMITER //  
> CREATE OR REPLACE FUNCTION `SORT_LIST`(I\_LIST longtext CHARACTER SET utf8 COLLATE utf8\_general\_ci, I\_OLD\_SEP longtext CHARACTER SET utf8 COLLATE utf8\_general\_ci, I\_NEW\_SEP longtext CHARACTER SET utf8 COLLATE utf8\_general\_ci) RETURNS longtext AS  
> DECLARE  
> V\_TEMP LONGTEXT;  
> A\_LIST ARRAY(LONGTEXT);  
> V\_LIST\_INDEX INT=-1;  
> V\_NEXT\_SEP INT;  
> V\_SORTED\_LIST LONGTEXT;
> 
> BEGIN  
> V\_TEMP=I\_LIST;  
> V\_NEXT\_SEP=INSTR(V\_TEMP,I\_OLD\_SEP);
> 
> WHILE V\_NEXT\_SEP\>0 LOOP  
> A\_LIST+=CREATE\_ARRAY(1);  
> V\_LIST\_INDEX=V\_LIST\_INDEX+1;  
> A\_LIST[V\_LIST\_INDEX]=LEFT(V\_TEMP,V\_NEXT\_SEP-1);  
> V\_TEMP=SUBSTR(V\_TEMP,V\_NEXT\_SEP+1);  
> V\_NEXT\_SEP=INSTR(V\_TEMP,I\_OLD\_SEP);  
> END LOOP;
> 
> IF V\_TEMP IS NOT NULL THEN  
> V\_LIST\_INDEX=V\_LIST\_INDEX+1;  
> A\_LIST+=CREATE\_ARRAY(1);  
> A\_LIST[V\_LIST\_INDEX]=V\_TEMP;  
> END IF;
> 
> FOR i IN 0 … LENGTH(A\_LIST)-1 LOOP  
> FOR j IN 0 … LENGTH(A\_LIST)-i-2 LOOP  
> IF A\_LIST[j]\>A\_LIST[j+1] THEN  
> V\_TEMP=A\_LIST[j];  
> A\_LIST[j]=A\_LIST[j+1];  
> A\_LIST[j+1]=V\_TEMP;  
> ELSIF A\_LIST[j]=A\_LIST[j+1] THEN  
> A\_LIST[j+1]=NULL;  
> END IF;  
> END LOOP;  
> END LOOP;
> 
> FOR i IN 0 … LENGTH(A\_LIST)-1 LOOP  
> IF A\_LIST[i] IS NOT NULL THEN  
> IF i=0 THEN  
> V\_SORTED\_LIST=A\_LIST[i];  
> ELSE  
> V\_SORTED\_LIST=CONCAT(V\_SORTED\_LIST,I\_NEW\_SEP,A\_LIST[i]);  
> END IF;  
> END IF;  
> END LOOP;
> 
> RETURN V\_SORTED\_LIST;  
> END//  
> DELIMITER ;
