function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pok LauPok Lau 

Retreive range of values

These are the fields on my custom object
AUTO_ID(auto number) | version (text)
           1                           8.3.1
           2                           8.3.2
           3                           8.3.3
           4                           8.4.1

How can I display all the versions between 8.3.1 to 8.4.1.

As both the AUTO_ID and versions are of type string. Please help or suggest a best way to solve this problem.

 
Donald BlayDonald Blay
I think you will have to do a SOQL select to frist get them all. Then loop through each one, dissasembling the version numbers into point-number, and comparing them to the corrisponding component numbers in the version you're looking for.  If a version is out of the range; for example you're looking for a minor-point version between 3 - 4 (8.3.1 to 8.4.1) and the version number you're looking at is 2 (8.2.5) then add it to a list of items to exclude (if you find one number that's out of range, its no good and you can just add it to the exclusion list  and move on).  Then after you you finish looping through them, you can then loop through the master list again, if the record is not in the exclusion list, add it to another list, the list of items you want to return.  

To dissaemble the version numbers, I suggeest you use the String.Split() method and split it on the period.  You will then have an array of the 3 component numbers of the version.  So:
String thisVersionNumber = '8.3.1';
String[] versionComponents = thisVersionNumber.Split('\\.');
You will then end up with an 3-element array with [8], [3], and [1] in it.    And as a side-note, Split uses regular expression, so that's why you need the 2 slashes before the period.  

Below is some pseudo-code.  I have NOT complied it or tested it, so it might need some modification or fixes.  But it should be a good place for you to start.
 
String lowerVersionNumber = '8.3.1';
String upperVersionNumber = '8.4.1';

String[] lowerVersionArray = lowerVersionNumber.split('\\.');
String[] upperVersionArray = upperVersionNumber.split('\\.');

// Map of the ones we don't want, indexed by the Name/Auto-nuber.
List<String, Auto_Id__c> mapVersionsToExclude = new List<Auto_Id__c>();

// The final list of the ones we want
List<Auto_Id__c> lstVersionsToKeep = new List<Auto_Id__c>();

//Get all the version numbers, and we'll filter them down later
List<Auto_Id__c> lstAllVersionNumbers = [Select Name, Version from Auto_Id__c];

for(Auto_Id__c thisVersionNumber : lstAllVersionNumbers){
	String[] thisVersionArray = thisVersionNumber.split('\\.');
	Integer count = 0;
	//Now step through each component of the version number we're currently looking at and see if it's in range
	for(String versionComponent: thisVersionArray){
		// Convert all the numbers we're dealing with from Strings to Integers
		Integer versionComponentNum = Integer.valueof(versionComponent);
		
		// Use the counter to get the corrisponding conpoent of the version number
		Integer upperValue = Integer.valueof(upperVersionArray[count]);
		Integer lowerComponentNum = Integer.valueof(lowerVersionArray[count]);

		// If it's out of range at the current step, then add it to the exclusion list. 
		if(versionComponentNum < lowerComponentNum || versionComponentNum > upperValue){
			versionsToExclude.put(thisVersionNumber.Name, thisVersionNumber);
		}
	}
}

// Now that we have a list of the version that we don't want, let's build the list of the ones we do
for(Auto_Id__c thisVersionNumber : lstAllVersionNumbers){
	// It's NOT in the exclusion list, then add it to the list to keep
	if(mapVersionsToExclude.containsKey(thisVersionNumber.Name) == false){
		lstVersionsToKeep.add(thisVersionNumber);
	}
}

//Now you'll have the values that are in range in the list lstVersionsToKeep