<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Ordering fields in ASCII mode in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Ordering-fields-in-ASCII-mode/m-p/1530830#M438728</link>
    <description>&lt;P&gt;When sorting by text QlikView uses phonebook order (p1, p2, p10, p11) and not ASCII mode (p1, p10, p11, p2) as in many other systems.&amp;nbsp;&amp;nbsp;It treats sequence of digits as one number and orders strings depending on the value of the number. So e.g. "9" goes before "10" and not vice versa.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image.png" style="width: 314px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3819i4175ECBD1A5F290C/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;To have our strings ordered in ASCII mode at first we need to create an ordering field by copying the source field's content into new text field.&amp;nbsp;&amp;nbsp;Digits should be separated by non digit symbol. In the code below I separated all symbols, not only digits, by "-".&amp;nbsp;For example string "p100" becomes "p-1-0-0" in the ordering field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;In the next step we copy original field as is into a new and use the "separated" field for sorting. Because digits a separated they don't form multi-digit numbers and therefore every digit is treated as a single symbol in sorting.&lt;/P&gt;&lt;P&gt;There is an easier way to achieve ASCII ordering - select sort by "Expression" and use function Rank() as an ordering expression.&amp;nbsp;This solution suggested by Swuehl (thank you!) in the topic&amp;nbsp;&lt;A title="Listbox - text sort not ASCII?" href="https://community.qlik.com/t5/QlikView-App-Development/Listbox-text-sort-not-ASCII/td-p/560980" target="_self"&gt;Listbox - text sort not ASCII?&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;I modified the sorting expression to allow ordering of all values (selected as well as not active): "-Rank(Only({1} field))". Without set expression "{1}" only active records are ordered.&lt;/P&gt;&lt;P&gt;&amp;nbsp;I made a simple example application to show both approaches to ASCII sorting (see attached ASCII_sorting.qvw).&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image.png" style="width: 642px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3820i2DED906C7730CD7F/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Reloading of field's values in right ASCII order is put in separate subroutine "order_ASCII". It might be copied and used in other applications.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// Sorting a field in ASCII mode. The field will be loaded ordered in ASCII mode
// Parameters:
// p_field_name - a name of a field to order. Input value must be given as a string ('field name')
// p_table_name - a name of a table where the field exists. Input value must be given as a string ('table name')
// p_replace - recreate the field by reloading it in ASCII order: 1 - yes; 0 - no: new field will be created [$(p_field_name)_ordered] and sorted in ASCII mode
SUB order_ASCII(p_field_name, p_table_name, p_replace)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;See full source code of the subroutine under the spoiler below&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;// Sorting a field in ASCII mode. The field will be loaded ordered in ASCII mode
// Parameters:
// p_field_name - a name of a field to order. Input value must be given as a string ('field name')
// p_table_name - a name of a table where the field exists. Input value must be given as a string ('table name')
// p_replace - recreate the field by reloading it in ASCII order: 1 - yes; 0 - no: new field will be created [$(p_field_name)_ordered] and sorted in ASCII mode
SUB order_ASCII(p_field_name, p_table_name, p_replace)

	// Loading distinct values of the ordering field
	__ot:
	LOAD Distinct
		[$(p_field_name)] as p,
		// Text value of the field to be ordered by
		Text([$(p_field_name)]) as p_text
	Resident [$(p_table_name)];
	
	// Spliting strings into symbols
	__ot1:
	LOAD
		p,
		IterNo() as posn,
		Mid(p_text, IterNo(), 1) as symbol
	Resident __ot
	While IterNo() &amp;lt;= Len(p_text);
	
	DROP Table __ot;
	
	// Creating a text to get right ASCII ordering: concatenating symbols back into a string using "-" as a separator
	__ot:
	LOAD
		p,
		'-' &amp;amp; Concat(symbol, '-', posn) &amp;amp; '-' as p_sort
	Resident __ot1
	Group By p;
	
	DROP Table __ot1;
	
	// Adding to the source table new field as a copy of the source field. Using the previously created separated field in ordering
	// We need fresh new field because the source field is already loaded in some order which we didn't change
	Left Join ([$(p_table_name)])
	LOAD
		p as [$(p_field_name)],
		p as [$(p_field_name)_ordered]
	Resident __ot
	Order By p_sort;
	
	DROP Table __ot;
	
	// Replacing the source field by new one if needed
	IF p_replace = 1 THEN
		// Presuming that the source field is not a key (exists only in one table)
		DROP Field [$(p_field_name)] From [$(p_table_name)];
		RENAME Field [$(p_field_name)_ordered] to [$(p_field_name)];
	END IF;
END SUB;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;Victor.&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jan 2019 10:19:24 GMT</pubDate>
    <dc:creator>vicn1390</dc:creator>
    <dc:date>2019-01-16T10:19:24Z</dc:date>
    <item>
      <title>Ordering fields in ASCII mode</title>
      <link>https://community.qlik.com/t5/QlikView/Ordering-fields-in-ASCII-mode/m-p/1530830#M438728</link>
      <description>&lt;P&gt;When sorting by text QlikView uses phonebook order (p1, p2, p10, p11) and not ASCII mode (p1, p10, p11, p2) as in many other systems.&amp;nbsp;&amp;nbsp;It treats sequence of digits as one number and orders strings depending on the value of the number. So e.g. "9" goes before "10" and not vice versa.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image.png" style="width: 314px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3819i4175ECBD1A5F290C/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;To have our strings ordered in ASCII mode at first we need to create an ordering field by copying the source field's content into new text field.&amp;nbsp;&amp;nbsp;Digits should be separated by non digit symbol. In the code below I separated all symbols, not only digits, by "-".&amp;nbsp;For example string "p100" becomes "p-1-0-0" in the ordering field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;In the next step we copy original field as is into a new and use the "separated" field for sorting. Because digits a separated they don't form multi-digit numbers and therefore every digit is treated as a single symbol in sorting.&lt;/P&gt;&lt;P&gt;There is an easier way to achieve ASCII ordering - select sort by "Expression" and use function Rank() as an ordering expression.&amp;nbsp;This solution suggested by Swuehl (thank you!) in the topic&amp;nbsp;&lt;A title="Listbox - text sort not ASCII?" href="https://community.qlik.com/t5/QlikView-App-Development/Listbox-text-sort-not-ASCII/td-p/560980" target="_self"&gt;Listbox - text sort not ASCII?&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;I modified the sorting expression to allow ordering of all values (selected as well as not active): "-Rank(Only({1} field))". Without set expression "{1}" only active records are ordered.&lt;/P&gt;&lt;P&gt;&amp;nbsp;I made a simple example application to show both approaches to ASCII sorting (see attached ASCII_sorting.qvw).&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image.png" style="width: 642px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3820i2DED906C7730CD7F/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Reloading of field's values in right ASCII order is put in separate subroutine "order_ASCII". It might be copied and used in other applications.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// Sorting a field in ASCII mode. The field will be loaded ordered in ASCII mode
// Parameters:
// p_field_name - a name of a field to order. Input value must be given as a string ('field name')
// p_table_name - a name of a table where the field exists. Input value must be given as a string ('table name')
// p_replace - recreate the field by reloading it in ASCII order: 1 - yes; 0 - no: new field will be created [$(p_field_name)_ordered] and sorted in ASCII mode
SUB order_ASCII(p_field_name, p_table_name, p_replace)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;See full source code of the subroutine under the spoiler below&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;// Sorting a field in ASCII mode. The field will be loaded ordered in ASCII mode
// Parameters:
// p_field_name - a name of a field to order. Input value must be given as a string ('field name')
// p_table_name - a name of a table where the field exists. Input value must be given as a string ('table name')
// p_replace - recreate the field by reloading it in ASCII order: 1 - yes; 0 - no: new field will be created [$(p_field_name)_ordered] and sorted in ASCII mode
SUB order_ASCII(p_field_name, p_table_name, p_replace)

	// Loading distinct values of the ordering field
	__ot:
	LOAD Distinct
		[$(p_field_name)] as p,
		// Text value of the field to be ordered by
		Text([$(p_field_name)]) as p_text
	Resident [$(p_table_name)];
	
	// Spliting strings into symbols
	__ot1:
	LOAD
		p,
		IterNo() as posn,
		Mid(p_text, IterNo(), 1) as symbol
	Resident __ot
	While IterNo() &amp;lt;= Len(p_text);
	
	DROP Table __ot;
	
	// Creating a text to get right ASCII ordering: concatenating symbols back into a string using "-" as a separator
	__ot:
	LOAD
		p,
		'-' &amp;amp; Concat(symbol, '-', posn) &amp;amp; '-' as p_sort
	Resident __ot1
	Group By p;
	
	DROP Table __ot1;
	
	// Adding to the source table new field as a copy of the source field. Using the previously created separated field in ordering
	// We need fresh new field because the source field is already loaded in some order which we didn't change
	Left Join ([$(p_table_name)])
	LOAD
		p as [$(p_field_name)],
		p as [$(p_field_name)_ordered]
	Resident __ot
	Order By p_sort;
	
	DROP Table __ot;
	
	// Replacing the source field by new one if needed
	IF p_replace = 1 THEN
		// Presuming that the source field is not a key (exists only in one table)
		DROP Field [$(p_field_name)] From [$(p_table_name)];
		RENAME Field [$(p_field_name)_ordered] to [$(p_field_name)];
	END IF;
END SUB;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;Victor.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 10:19:24 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Ordering-fields-in-ASCII-mode/m-p/1530830#M438728</guid>
      <dc:creator>vicn1390</dc:creator>
      <dc:date>2019-01-16T10:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering fields in ASCII mode</title>
      <link>https://community.qlik.com/t5/QlikView/Ordering-fields-in-ASCII-mode/m-p/1530839#M438729</link>
      <description>&lt;P&gt;And there is another way to achieve ASCII sorting - use expression "Aggr(Only({1} field), field)" as field and select "Text" as sort by condition.&amp;nbsp;We have ASCII sorting instead of phonebook in this case. Looks like a bug but it works as needed.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 827px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3828iDC0FB4290DBE5E51/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 684px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/3829iC2AD0B13E1B6CBE0/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 10:30:46 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Ordering-fields-in-ASCII-mode/m-p/1530839#M438729</guid>
      <dc:creator>vicn1390</dc:creator>
      <dc:date>2019-01-16T10:30:46Z</dc:date>
    </item>
  </channel>
</rss>

